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

same_item_push lint can trigger when the pushed value is not constant. #5902

Closed
Herschel opened this issue Aug 13, 2020 · 2 comments · Fixed by #5908
Closed

same_item_push lint can trigger when the pushed value is not constant. #5902

Herschel opened this issue Aug 13, 2020 · 2 comments · Fixed by #5908
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@Herschel
Copy link
Contributor

Herschel commented Aug 13, 2020

Simple example:

    let mut out = Vec::new();
    let mut i = 0;
    for _ in 0..100 {
        out.push(i);
        i += 10;
    }
    dbg!(out);

I expected to see this happen:

No clippy warning, because the value being pushed is not constant (despite the otherwise unidiomatic code, as this is a contrived example).

Instead, this happened:
The same_item_push lint throws a warning with an incorrect description of a fix.

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

warning: 1 warning emitted

The docs at https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push say that this lint should only fire when "a for loop is used to push a constant value", but in this case the value being pushed is not constant.

Meta

  • cargo clippy -V: clippy 0.0.212 (576d27c 2020-08-12)
  • rustc -Vv:

rustc 1.47.0-nightly (576d27c 2020-08-12)
binary: rustc
commit-hash: 576d27c
commit-date: 2020-08-12
host: x86_64-pc-windows-msvc
release: 1.47.0-nightly
LLVM version: 10.0

@Herschel Herschel added the C-bug Category: Clippy is not doing the correct thing label Aug 13, 2020
@giraffate
Copy link
Contributor

I will prepare a fix.

@repi
Copy link

repi commented Aug 14, 2020

We just ran into this as well with rustc 1.47.0-nightly (81dc88f 2020-08-13)

repro:

fn main() {
    let vertlist = [0,1,2,3];
    let mut indices = vec![];
    let mut i = 0;

    for _ in 0..3 {
        let vidx = vertlist[i];
        indices.push(vidx);
        i += 1;
    }
}

causes:

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

@bors bors closed this as completed in 9360ca6 Aug 17, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 1, 2020
[beta][clippy] backport multiple FP fixes for a warn-by-default lint

This backports the PR rust-lang/rust-clippy#6016 fixing multiple FPs:

rust-lang/rust-clippy#5902
rust-lang/rust-clippy#5979
rust-lang/rust-clippy#5985

We didn't have any complaints about this lint, since me merged this PR.

cc `@ebroto` (sorry I forgot about this, since we talked about the backport 3 weeks ago 😐)

r? `@pietroalbini`
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

Successfully merging a pull request may close this issue.

3 participants