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

Bad suggestion of useless_let_if_seq when having side effects. #4124

Open
oxalica opened this issue May 21, 2019 · 0 comments
Open

Bad suggestion of useless_let_if_seq when having side effects. #4124

oxalica opened this issue May 21, 2019 · 0 comments
Labels
L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@oxalica
Copy link
Contributor

oxalica commented May 21, 2019

The version of clippy is 0.0.212.

Here's an example:

pub fn func() {
    // This need to be called first always.
    let mut ret = foo1();
    if cond() {
        // Overwrite with some complex expr.
        ret = (foo2() * 2).to_string().len() as _;
    }
    
    baz(ret)
}

It suggest writing let <mut> ret = if cond() { .. foo2() .. } else { foo1() };, which cause 3 function calls (may have side-effects) being reordered, and foo1() not always being called.

Well, I'm not sure how to write make it more idiomatic in this case...

@flip1995 flip1995 added the L-suggestion Lint: Improving, adding or fixing lint suggestions label May 21, 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
L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

2 participants