You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of rust-lang#11865 - yuxqiu:map_unwrap_or_default, r=Jarcho
feat: add `manual_is_variant_and` lint
changelog: add a new lint [`manual_is_variant_and`].
- Replace `option.map(f).unwrap_or_default()` and `result.map(f).unwrap_or_default()` with `option.is_some_and(f)` and `result.is_ok_and(f)` where `f` is a function or closure that returns `bool`.
- MSRV is set to 1.70.0 for this lint; when `is_some_and` and `is_ok_and` was stabilised
---
For example, for the following code:
```rust
let opt = Some(0);
opt.map(|x| x > 1).unwrap_or_default();
```
It suggests to instead write:
```rust
let opt = Some(0);
opt.is_some_and(|x| x > 1)
```
Copy file name to clipboardExpand all lines: clippy_lints/src/methods/mod.rs
+35-1
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,7 @@ mod iter_skip_zero;
51
51
mod iter_with_drain;
52
52
mod iterator_step_by_zero;
53
53
mod join_absolute_paths;
54
+
mod manual_is_variant_and;
54
55
mod manual_next_back;
55
56
mod manual_ok_or;
56
57
mod manual_saturating_arithmetic;
@@ -3829,6 +3830,32 @@ declare_clippy_lint! {
3829
3830
"filtering an iterator over `Result`s for `Ok` can be achieved with `flatten`"
3830
3831
}
3831
3832
3833
+
declare_clippy_lint!{
3834
+
/// Checks for usage of `option.map(f).unwrap_or_default()` and `result.map(f).unwrap_or_default()` where f is a function or closure that returns the `bool` type.
3835
+
///
3836
+
/// ### Why is this bad?
3837
+
/// Readability. These can be written more concisely as `option.is_some_and(f)` and `result.is_ok_and(f)`.
3838
+
///
3839
+
/// ### Example
3840
+
/// ```no_run
3841
+
/// # let option = Some(1);
3842
+
/// # let result: Result<usize, ()> = Ok(1);
3843
+
/// option.map(|a| a > 10).unwrap_or_default();
3844
+
/// result.map(|a| a > 10).unwrap_or_default();
3845
+
/// ```
3846
+
/// Use instead:
3847
+
/// ```no_run
3848
+
/// # let option = Some(1);
3849
+
/// # let result: Result<usize, ()> = Ok(1);
3850
+
/// option.is_some_and(|a| a > 10);
3851
+
/// result.is_ok_and(|a| a > 10);
3852
+
/// ```
3853
+
#[clippy::version = "1.76.0"]
3854
+
pubMANUAL_IS_VARIANT_AND,
3855
+
pedantic,
3856
+
"using `.map(f).unwrap_or_default()`, which is more succinctly expressed as `is_some_and(f)` or `is_ok_and(f)`"
0 commit comments