-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
FP fix and documentation for branches_sharing_code
lint
#7462
FP fix and documentation for branches_sharing_code
lint
#7462
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
// ######################### | ||
// # Issue 7452 | ||
// ######################### | ||
fn test() { | ||
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; | ||
let mut counter = 0; | ||
for i in v { | ||
if counter == 0 { | ||
counter += 1; | ||
println!("first"); | ||
} else if counter == 1 { | ||
counter += 1; | ||
println!("second"); | ||
} else { | ||
counter += 1; | ||
println!("other: {}", i); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the issue that is not fixed yet. I found it reasonable to add a test for it (even if it fails) as it's also documented in the lint description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we gain anything by adding this. Tests are valuable when they verify correct behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wasn't sure with this one that's, the reason why I pointed it out. I'll remove it from this PR and then squash the two commits 🙃
clippy_lints/src/copies.rs
Outdated
@@ -225,7 +228,7 @@ fn lint_same_then_else<'tcx>( | |||
blocks, | |||
expr, | |||
); | |||
} else if end_eq != 0 || (has_expr && expr_eq) { | |||
} else if (end_eq != 0 && !has_expr) || (has_expr && expr_eq) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the real problem is that scan_block_for_eq
can return end_eq > 0 && expr_eq == false
? End statements should not be evaluated if the expressions are not equal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. scan_block_for_eq
even contained an if block for this purpose, but the condition was not quite correct, it's fixed in the latest version. 🙃
For #7452, I would maybe first collect all the variables referenced in the condition. Then, when determining if statements are equal, if there is a mutable reference to the variable, the statements are not equal. So I think you would add |
Interesting idea, I thought about expending the |
20de4f2
to
bd2e7ef
Compare
The FP is now fixed in |
bd2e7ef
to
6bfbe5c
Compare
And added `branches_sharing_code` PF note to lint doc for `rust-clippy#7452`
6bfbe5c
to
61e2808
Compare
Yeah I know what you mean. That would work. One more idea for |
Looks good, thanks! @bors r+ |
📌 Commit 61e2808 has been approved by |
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Closes #7369
Related #7452 I'm still thinking about the best way to fix this. I could simply add another visitor to ensure that the moved expressions don't modify values being used in the condition, but I'm not totally happy with this due to the complexity. I therefore only documented it for now
changelog: [
branches_sharing_code
] fixed false positive where block expressions would sometimes be ignored.