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

Fix FP in same_item_push #5997

Merged

Conversation

giraffate
Copy link
Contributor

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

Don't emit a lint when the pushed item doesn't have Clone trait
@rust-highfive
Copy link

r? @flip1995

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 1, 2020
@ebroto
Copy link
Member

ebroto commented Sep 4, 2020

r? @ebroto

(Will dig into this later)

@rust-highfive rust-highfive assigned ebroto and unassigned flip1995 Sep 4, 2020
Copy link
Member

@ebroto ebroto left a 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?

clippy_lints/src/loops.rs Outdated Show resolved Hide resolved
@giraffate
Copy link
Contributor Author

@ebroto Thanks for your comment! I agree with restricting same_item_push to suppress many false positives.

I think we should restrict same_item_push to just literals and constants (and immutable bindings that are initialized with those).

I have a question. In the above restriction, does this test emit a lint? This pattern is mentioned in the original issue #4078 of same_item_push.

// Test for basic case
let mut spaces = Vec::with_capacity(10);
for _ in 0..10 {
spaces.push(vec![b' ']);
}

Would you be up to do that change, either in this PR or in a follow-up?

I will prepare a follow-up PR.

@ebroto
Copy link
Member

ebroto commented Sep 5, 2020

I have a question. In the above restriction, does this test emit a lint? This pattern is mentioned in the original issue #4078 of same_item_push.

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 resize() and vec![x; N].

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);
    }
}

(playground)

I will prepare a follow-up PR.

Great, thanks in advance!

@ebroto
Copy link
Member

ebroto commented Sep 5, 2020

@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Sep 5, 2020

📌 Commit 96b31a5 has been approved by ebroto

@bors
Copy link
Contributor

bors commented Sep 5, 2020

⌛ Testing commit 96b31a5 with merge e9440cb...

@bors
Copy link
Contributor

bors commented Sep 5, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: ebroto
Pushing e9440cb to master...

@bors bors merged commit e9440cb into rust-lang:master Sep 5, 2020
@giraffate giraffate deleted the fix_fp_about_clone_in_same_item_push branch September 6, 2020 13:35
@giraffate
Copy link
Contributor Author

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 resize() and vec![x; N].

Ok, I agree with you. Thanks!

bors added a commit that referenced this pull request Sep 8, 2020
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`
bors added a commit that referenced this pull request Oct 9, 2020
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

same_item_push being suggested for values that are not Copy or Clone
5 participants