Skip to content

vec_init_then_push should only trigger if there are no further pushes #7071

Closed
@jyn514

Description

@jyn514

Lint name: vec_init_then_push

I tried this code:

    let mut arg_strings: Vec<Box<str>> = Vec::new();
    arg_strings.push(name.to_owned().into_boxed_str());
    for arg in args {
        arg_strings.push(
            arg.as_ref()
                .to_os_string()
                .into_string()
                .unwrap()
                .into_boxed_str(),
        );
    }

I expected to see this happen: The lint doesn't trigger, because using vec![] and then immediately pushing doesn't improve the code any, it uses two different styles of initializing the vector.

Instead, this happened:

warning: calls to `push` immediately after creation
   --> tests/mock/clitools.rs:613:5
    |
613 | /     let mut arg_strings: Vec<Box<str>> = Vec::new();
614 | |     arg_strings.push(name.to_owned().into_boxed_str());
    | |_______________________________________________________^ help: consider using the `vec![]` macro: `let mut arg_strings: Vec<Box<str>> = vec![..];`

cc rust-lang/rustup#2718, @kinnison

Meta

  • cargo clippy -V: clippy 0.1.53 (dae9d6a 2021-04-09)
  • rustc -v: rustc 1.53.0-nightly (dae9d6a 2021-04-09)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions