Skip to content

vec_init_then_push incorrectly fires when the push() is in a loop body #7019

@duci9y

Description

@duci9y

Lint name: vec_init_then_push

I tried this code:

let mut v = Vec::new();
loop {
    match *s {
        ...
        SExp::Cons(ref a, ref b) => {
            v.push(a);
        }
        ...
    }
}

I expected to see this happen: The lint to not fire, since this is a valid use case for init then push.

Instead, this happened: The lint fired.

378 | /         let mut v = Vec::new();
379 | |         loop {
380 | |             match *s {
381 | |                 ...
382 | |                 SExp::Cons(ref a, ref b) => {
383 | |                     v.push(a);
    | |______________________________^ help: consider using the `vec![]` macro: `let mut v = vec![..];`

Meta

  • cargo clippy -V: clippy 0.1.51 (2fd73fab 2021-03-23)
  • rustc -Vv:
rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-apple-darwin
release: 1.51.0
LLVM version: 11.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-needs-mcveCall for participation: This issue needs a Minimal Complete and Verifiable ExampleI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions