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

useless_let_if_seq should not warn when variable is set from multiple places. #3769

Open
kevincox opened this issue Feb 16, 2019 · 3 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-style Lint: Belongs in the style lint group

Comments

@kevincox
Copy link

I have some code that looks like this.

let ok = true;

if check1() {
  // Log some info.
  ok = false;
}

if check2() {
  // Log some info.
  ok = false;
}

if check3() {
  // Log some info.
  ok = false;
}

However clippy gives me a useless_let_if_seq saying that I should merge the first if into the let. I don't think this is a helpful suggestion because 1) It makes the first check look special and 2) it distracts a bit from the true until marked false.

After thinking about this a bit I think that if the variable defined from the let is set from multiple locations in the code then this warning should probably be suppressed.

@flip1995
Copy link
Member

Duplicate of #3768 and #2749

@kevincox
Copy link
Author

These are different. The linked bug is about setting multiple variables where this bug is about setting one variable from multiple locations.

@flip1995
Copy link
Member

Oh yeah, you're right! Reopening it, sorry.

@flip1995 flip1995 reopened this Feb 19, 2019
@flip1995 flip1995 added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-style Lint: Belongs in the style lint group labels Feb 19, 2019
bors added a commit that referenced this issue May 15, 2020
Downgrade useless_let_if_seq to nursery

I feel that this lint has the wrong balance of incorrect suggestions for a default-enabled lint.

The immediate code I faced was something like:

```rust
fn main() {
    let mut good = do1();
    if !do2() {
        good = false;
    }
    if good {
        println!("good");
    }
}

fn do1() -> bool { println!("1"); false }
fn do2() -> bool { println!("2"); false }
```

On this code Clippy calls it unidiomatic and suggests the following diff, which has different behavior in a way that I don't necessarily want.

```diff
- let mut good = do1();
- if !do2() {
-     good = false;
- }
+ let good = if !do2() {
+     false
+ } else {
+     do1()
+ };
```

On exploring issues filed about this lint, I have found that other users have also struggled with inappropriate suggestions (#4124, #3043, #2918, #2176) and suggestions that make the code worse (#3769, #2749). Overall I believe that this lint is still at nursery quality for now and should not be enabled.

---

changelog: Remove useless_let_if_seq from default set of enabled lints
bors added a commit that referenced this issue May 15, 2020
Downgrade useless_let_if_seq to nursery

I feel that this lint has the wrong balance of incorrect suggestions for a default-enabled lint.

The immediate code I faced was something like:

```rust
fn main() {
    let mut good = do1();
    if !do2() {
        good = false;
    }
    if good {
        println!("good");
    }
}

fn do1() -> bool { println!("1"); false }
fn do2() -> bool { println!("2"); false }
```

On this code Clippy calls it unidiomatic and suggests the following diff, which has different behavior in a way that I don't necessarily want.

```diff
- let mut good = do1();
- if !do2() {
-     good = false;
- }
+ let good = if !do2() {
+     false
+ } else {
+     do1()
+ };
```

On exploring issues filed about this lint, I have found that other users have also struggled with inappropriate suggestions (#4124, #3043, #2918, #2176) and suggestions that make the code worse (#3769, #2749). Overall I believe that this lint is still at nursery quality for now and should not be enabled.

---

changelog: Remove useless_let_if_seq from default set of enabled lints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-style Lint: Belongs in the style lint group
Projects
None yet
Development

No branches or pull requests

2 participants