-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Incorrect suggestion by useless_let_if_seq
lint
#2918
Comments
It does not understand when return, break, continue are used. Relevant: rust-lang/rust-clippy#2918
Hi, here is another incorrect suggestion: fn main() {
let v: Vec<String> = vec![];
let mut x = 0;
if v.iter().any(|s| s.len() > x) {
x = 5;
}
dbg!(x);
} suggestion:
which is wrong since x wont be initialized in the condition |
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
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
Ran into a case today where the suggestion generated is also incorrect, source: let mut changed = false;
if self.pos != p {
self.pos = p;
changed = true;
}
if self.rot != r {
self.rot = r;
changed = true;
}
if changed {
// do the thing
} suggestion:
The suggestion incorrectly changes the flow control to only update |
Retested on latest nightly |
Hi,
Given the following code:
clippy suggests the following:
This is incorrect, since
x
is modified in the loop in the else path, but NOT assigned tosx
.(Playground link)
(Note: the code itself is not the most idiomatic, since I'm transcribing an algorithm from another language...)
The text was updated successfully, but these errors were encountered: