- Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thing
Description
Summary
The option_if_let_else
suggestions do not check if the closure can be replaced by the function itself.
Reproducer
I tried this code:
#[warn(clippy::option_if_let_else)] fn main() { let option: Option<&std::collections::HashMap<&str, &str>> = None; let mut hashmap = if let Some(hashmap) = option { hashmap.clone() } else { std::collections::HashMap::new() }; hashmap.insert("Hello", "Clippy"); println!("{hashmap:?}"); }
I expected to see this happen:
warning: use Option::map_or_else instead of an if let/else --> src/main.rs:4:23 | 4 | let mut hashmap = if let Some(hashmap) = option { | _______________________^ 5 | | hashmap.clone() 6 | | } else { 7 | | std::collections::HashMap::new() 8 | | }; | |_____^ help: try: `option.map_or_else(|| std::collections::HashMap::new(), |hashmap| hashmap.clone())` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
After applying the fix I expect no warnings from Clippy.
Instead, this happened:
warning: redundant closure --> src/main.rs:4:42 | 4 | let mut hashmap = option.map_or_else(|| std::collections::HashMap::new(), |hashmap| hashmap.clone()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `std::collections::HashMap::new` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure = note: `#[warn(clippy::redundant_closure)]` on by default
Version
rustc 1.74.0-nightly (84a9f4c6e 2023-08-29) binary: rustc commit-hash: 84a9f4c6e6f0f02bff9acc9a5e0305a1554a87fc commit-date: 2023-08-29 host: x86_64-unknown-linux-gnu release: 1.74.0-nightly LLVM version: 17.0.0
Additional Labels
@rustbot label +L-suggestion
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thing