Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if_assert #13725

Open
Rudxain opened this issue Nov 25, 2024 · 0 comments
Open

if_assert #13725

Rudxain opened this issue Nov 25, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@Rudxain
Copy link
Contributor

Rudxain commented Nov 25, 2024

What it does

It should deny or warn when a predicate is checked by both an assert (or any similar built-in macro) and any conditional control-flow construct, in any order

Advantage

  • It removes a redundant check that's certainly a bug
  • Less confusion for readers
  • No change in code-gen, as the optimizer could catch the dupe and eliminate it

Drawbacks

  • False negatives because of custom macros
  • Cannot be auto-fixed, because intent isn't clear (wrong?)

Example

fn f<T: std::fmt::Debug + PartialEq>(a: T, b: T) {
    if a == b {
        assert_eq!(a, b);
        // do something
    }
}

fn g<T: std::fmt::Debug + PartialEq>(a: T, b: T) {
    assert_eq!(a, b);
    if a == b {
        // do something
    }
}

Could be written as:

fn f<T: std::fmt::Debug + PartialEq>(a: T, b: T) {
    if a == b {
        // do something
    }
}

fn g<T: std::fmt::Debug + PartialEq>(a: T, b: T) {
    assert_eq!(a, b);
    // do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant