Skip to content

False positive useless_vec when vec argument is used twice in a macro #11861

Closed
@synek317

Description

@synek317

Summary

If a Vec is passed to a macro that uses its argument more than once and one usage could indeed be replaced with an array but the other could not, the useless_vec lint triggers a false positive.

Link to the playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e6a7db5f00b948def8a3a836f98ad071

Lint Name

useless_vec

Reproducer

I tried this code:

fn foo(_bar: usize, _baz: Vec<usize>) {}

macro_rules! baz {
    ($x:expr) => {{
        foo($x.iter().sum(), $x)
    }};
}

fn main() {
    baz!(vec![1])    
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: useless use of `vec!`
  --> src/main.rs:10:10
   |
10 |     baz!(vec![1])    
   |          ^^^^^^^ help: you can use an array directly: `[1]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
   = note: `#[warn(clippy::useless_vec)]` on by default

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s

I expected to see this happen:

    Checking playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.35s

Version

1.76.0-nightly (2023-11-22 1e9dda77b5b8e690c7e2)

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingE-help-wantedCall for participation: Help is requested to fix this issue.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