-
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
Fix FP in same_item_push
#5997
Fix FP in same_item_push
#5997
Conversation
Don't emit a lint when the pushed item doesn't have Clone trait
r? @flip1995 (rust_highfive has picked a reviewer for you, use r? to override) |
r? @ebroto (Will dig into this later) |
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.
Besides the requested change, I think we should restrict same_item_push
to just literals and constants (and immutable bindings that are initialized with those). Allowing the pushed item to be an arbitrary expression is very difficult to get right as there are many possibilities for false positives. For example, the very same function used in the test can return different values when called multiple times.
Would you be up to do that change, either in this PR or in a follow-up?
@ebroto Thanks for your comment! I agree with restricting
I have a question. In the above restriction, does this test emit a lint? This pattern is mentioned in the original issue #4078 of rust-clippy/tests/ui/same_item_push.rs Lines 13 to 17 in f98ffa2
I will prepare a follow-up PR. |
I think we should keep it simple for now and not special case this. We can always extend the lint later, but IMO the simpler approach will still be valuable to those that are not familiar with These are the cases I had in mind: const VALUE: usize = 42; // doesn't matter how it's initialized
fn main() {
let mut vec = Vec::new();
for _ in 0..20 {
vec.push(42); // literal
}
let mut vec = Vec::new();
for _ in 0..20 {
vec.push(VALUE); // constant
}
// For bindings, we should check if shadowing is involved
let mut vec = Vec::new();
let x = 42; // immutable, initialized with literal, could be inside the loop
for _ in 0..20 {
vec.push(x);
}
let mut vec = Vec::new();
let x = VALUE; // immutable, initialized with constant, could be inside the loop
for _ in 0..20 {
vec.push(x);
}
}
Great, thanks in advance! |
@bors r+ Thanks! |
📌 Commit 96b31a5 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Ok, I agree with you. Thanks! |
Restrict `same_item_push` to suppress false positives It only emits a lint when the pushed item is a literal, a constant and an immutable binding that are initialized with those, as discussed in #5997 (review). Fix #5985 changelog: Restrict `same_item_push` to literals, constants and immutable bindings that are initialized with those. r? `@ebroto`
Add changelog for 1.48 beta [Rendered](https://github.com/ebroto/rust-clippy/blob/changelog_1_48/CHANGELOG.md) I've not added the PRs fixing `same_item_push` because those were backported, namely: * [#5908](#5908) * [#5997](#5997) * [#6016](#6016) The following PR was reverted, so I've ignored it too: * [#5984](#5984) ~~Also, I took the liberty of adding a "Thanks" section, naming all the contributors to this release. I think they deserve visibility in the changelog. Please tell me if we want to add this or maybe it's redundant given we link to the PRs?~~ changelog: none r? `@flip1995`
Don't emit a lint when the pushed item doesn't have Clone trait
Fix #5979
changelog: Fix FP in
same_item_push
not to emit a lint when the pushed item doesn't have Clone trait