Skip to content

Clippy 1.63 fails to detect vec-init-then-push #9364

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

Closed
junglie85 opened this issue Aug 23, 2022 · 1 comment
Closed

Clippy 1.63 fails to detect vec-init-then-push #9364

junglie85 opened this issue Aug 23, 2022 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@junglie85
Copy link

junglie85 commented Aug 23, 2022

Summary

Clippy 1.63 fails to detect vec-init-then-push but it is detected with 1.62. This seems to only happen when there is an if statement after the initial push that conditionally pushes a subsequent element to the vec.

Lint Name

vec-init-then-push

Reproducer

I tried this code:

fn main() {
    let vec = vec_init_then_push(false);
    dbg!(vec);
}

fn vec_init_then_push(some_condition: bool) -> Vec<usize> {
    let mut vec = Vec::new();
    vec.push(1);

    if some_condition {
        vec.push(2);
    }

    vec
}

I expected to see this happen:

cargo clippy -- -D warnings
    Checking bad-clippy v0.1.0 (/Users/junglie85/bad-clippy)
error: calls to `push` immediately after creation
 --> src/main.rs:7:5
  |
7 | /     let mut vec = Vec::new();
8 | |     vec.push(1);
  | |________________^ help: consider using the `vec![]` macro: `let mut vec = vec![..];`
  |
  = note: `-D clippy::vec-init-then-push` implied by `-D warnings`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#vec_init_then_push

error: could not compile `bad-clippy` due to previous error

Instead, this happened:

cargo clippy -- -D warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s

Version

False negative:

rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: aarch64-apple-darwin
release: 1.63.0
LLVM version: 14.0.5

Detects:

rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: aarch64-apple-darwin
release: 1.62.1
LLVM version: 14.0.5
@junglie85 junglie85 added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Aug 23, 2022
@junglie85
Copy link
Author

I swear #7071 didn't come up when I first searched. But it makes sense and I see the fix was merged a couple of months ago. The new behaviour is preferable and my false positive bug is only really a bug if the behavious is supposed to be unchanged. I'll close.

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 I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

1 participant