Skip to content
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

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

Closed
remi-dupre opened this issue Jul 5, 2021 · 2 comments
Closed

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

remi-dupre opened this issue Jul 5, 2021 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@remi-dupre
Copy link

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
    
@remi-dupre remi-dupre added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 5, 2021
@remi-dupre
Copy link
Author

I've just noticed that the issue doesn't show up when using the nightly: cargo +nightly clippy. Does this mean that the issue is already fixed and will be available in next release?

@giraffate
Copy link
Contributor

Yes, it will be fixed at 1.54.0. FYI changelog is here: https://github.com/rust-lang/rust-clippy/blob/59a164e86c41b4ccfa1e41ec60d7bb8a0f2c87d2/CHANGELOG.md#false-positive-fixes.

I'm closing this. Anyway, thanks for the report!

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-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants