Skip to content

clippy::redundant_closure: false positive when calling vec![] #7435

Closed
@remi-dupre

Description

@remi-dupre

Hi!

Here is a minimal example to highlight the issue:

pub fn get_result() -> Result<Vec<i32>, String> {
    Ok(vec![0, 2, 4])
}

pub fn weird() {
    // This line triggers clippy::redundant_closure:
    // warning: redundant closure
    // --> src/lib.rs:6:42
    //  |
    //  |     let _x = get_result().unwrap_or_else(|_| vec![]);
    //  |                                          ^^^^^^^^^^ help: replace the closure with `Vec::new`: `std::vec::Vec::new`
    //  |
    //  = note: `#[warn(clippy::redundant_closure)]` on by default
    //  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
    let _x = get_result().unwrap_or_else(|_| vec![]);

    // This line doesn't trigger any warning
    let _y = get_result().unwrap_or_else(|_| Vec::new());
    let _z = get_result().unwrap_or_else(|_| vec![0]);
}

clippy suggests to update the code with something invalid as Vec::new is not equivalent to |_| vec![] which takes a parameter.

I think this may be related to #3071 but still seems to be a different issue since the vec![] macro call appears to be what causes it (there is no problem using Vec::new()).

Meta

  • cargo clippy -V: clippy 0.1.53 (53cb7b0 2021-06-17)
  • rustc -Vv:
    rustc 1.53.0 (53cb7b09b 2021-06-17)
    binary: rustc
    commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
    commit-date: 2021-06-17
    host: x86_64-unknown-linux-gnu
    release: 1.53.0
    LLVM version: 12.0.1
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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