mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
0b63e95dce
[`manual_filter_map`]: lint on `matches` and pattern matching Fixes #8010 Previously this lint only worked specifically for a very limited set of methods on the filter call (`.filter(|opt| opt.is_some())` and `.filter(|res| res.is_ok())`). This PR extends it to also recognize `matches!` in the `filter` and pattern matching with `if let` or `match` in the `map`. Example: ```rs enum Enum { A(i32), B, } let _ = [Enum::A(123), Enum::B].into_iter() .filter(|x| matches!(x, Enum::A(_))) .map(|x| if let Enum::A(s) = x { s } else { unreachable!() }); ``` Now suggests: ```diff - .filter(|x| matches!(x, Enum::A(_))).map(if let Enum::A(s) = x { s } else { unreachable!() }) + .filter_map(|x| match x { Enum::A(s) => Some(s), _ => None }) ``` Adding this required a somewhat large change in code because it originally seemed to be specifically written with only method calls in the filter in mind, and `matches!` has different behavior in the map, so this new setup should make it possible to support more "generic" cases that need different handling for the filter and map calls. changelog: [`manual_filter_map`]: lint on `matches` and pattern matching (and some internal refactoring) |
||
---|---|---|
.. | ||
src | ||
Cargo.toml |