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

False report on same item is being pushed into this Vec #6223

Closed
himajin2045 opened this issue Oct 25, 2020 · 1 comment
Closed

False report on same item is being pushed into this Vec #6223

himajin2045 opened this issue Oct 25, 2020 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@himajin2045
Copy link

I tried this code:

fn main() {
    let v1 = vec![Some(1), Some(2), Some(3), None];
    let mut v1_iter = v1.iter();
    let mut v2 = Vec::new();
    for _ in 0..3 {
        let a = match v1_iter.next() {
            Some(a) => a,
            None => break,
        };
        v2.push(a);
    }
    println!("items are not the same: {:#?}", v2);
}

I expected to see this happen:

No warnings.

Instead, this happened:

Clippy warns:

warning: it looks like the same item is being pushed into this Vec
  --> src/main.rs:10:9
   |
10 |         v2.push(a);
   |         ^^
   |
   = note: `#[warn(clippy::same_item_push)]` on by default
   = help: try using vec![a;SIZE] or v2.resize(NEW_SIZE, a)
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push

warning: 1 warning emitted

Meta

  • cargo clippy -V: clippy 0.0.212 (7e6d6e5 2020-08-16)
  • rustc -Vv:
rustc 1.47.0-nightly (7e6d6e5f5 2020-08-16)
binary: rustc
commit-hash: 7e6d6e5f535321c2223f044caba16f97b825009c
commit-date: 2020-08-16
host: x86_64-apple-darwin
release: 1.47.0-nightly
LLVM version: 10.0
Backtrace

<backtrace>

@himajin2045 himajin2045 added the C-bug Category: Clippy is not doing the correct thing label Oct 25, 2020
@ebroto
Copy link
Member

ebroto commented Oct 25, 2020

Hey @himajin2045, thanks for your report!

I can't reproduce this false positive in the playground.

This was fixed in #6016, and backported to 1.47 in rust-lang/rust#77403. Your Clippy version seems older (2020-08-16), so I think that if you update Clippy you will get rid of that false positive.

Feel free to reopen if the suggested fix does not work for you!

@ebroto ebroto closed this as completed Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

2 participants