Open df5602 opened 6 years ago
Hi, here is another incorrect suggestion: code:
fn main() {
let v: Vec<String> = vec![];
let mut x = 0;
if v.iter().any(|s| s.len() > x) {
x = 5;
}
dbg!(x);
}
suggestion:
help: it is more idiomatic to write: `let <mut> x = if v.iter().any(|s| s.len() > x) { 5 } else { 0 };`
which is wrong since x wont be initialized in the condition
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:
help: it is more idiomatic to write
|
106 | let <mut> changed = if self.rot != r { ..; true } else { if self.pos != p {
107 | self.pos = p;
108 | true
109 | } else {
110 | false
111 | } };
The suggestion incorrectly changes the flow control to only update self.pos
when self.rot
is not changed, compared to the original code
Retested on latest nightly clippy 0.1.57 (aa8f2d43 2021-09-18)
and this issue is still valid.
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...)