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

Suggest is_some_and for map(<f>).unwrap_or(false) #10102

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(drain_filter)]
#![feature(is_some_and)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
#![feature(lint_reasons)]
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ declare_clippy_lint! {
/// # let result: Result<usize, ()> = Ok(1);
/// # fn some_function(foo: ()) -> usize { 1 }
/// option.map(|a| a + 1).unwrap_or(0);
/// option.map(|a| a > 5).unwrap_or(false);
/// result.map(|a| a + 1).unwrap_or_else(some_function);
/// ```
///
Expand All @@ -490,6 +491,7 @@ declare_clippy_lint! {
/// # let result: Result<usize, ()> = Ok(1);
/// # fn some_function(foo: ()) -> usize { 1 }
/// option.map_or(0, |a| a + 1);
/// option.is_some_and(|a| a > 5);
/// result.map_or_else(some_function, |a| a + 1);
/// ```
#[clippy::version = "1.45.0"]
Expand Down
33 changes: 29 additions & 4 deletions clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_copy;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_path, Visitor};
use rustc_hir::{self, HirId, Path};
use rustc_hir::{self, ExprKind, HirId, Path};
use rustc_lint::LateContext;
use rustc_middle::hir::nested_filter;
use rustc_span::source_map::Span;
Expand Down Expand Up @@ -51,16 +52,34 @@ pub(super) fn check<'tcx>(
return;
}

let mut use_is_some_and_suggestion = false;

// argument to unwrap_or is boolean literal with value false, so is_some_and function is most
// appropriate
if let ExprKind::Lit(unwrap_lit) = &unwrap_arg.kind {
if let ast::LitKind::Bool(false) = &unwrap_lit.node {
use_is_some_and_suggestion = true;
}
}

let mut applicability = Applicability::MachineApplicable;
// get snippet for unwrap_or()
let unwrap_snippet = snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability);
// lint message
// comparing the snippet from source to raw text ("None") below is safe
// because we already have checked the type.
let arg = if unwrap_snippet == "None" { "None" } else { "<a>" };
let arg = if unwrap_snippet == "None" {
"None"
} else if use_is_some_and_suggestion {
"false"
} else {
"<a>"
};
let unwrap_snippet_none = unwrap_snippet == "None";
let suggest = if unwrap_snippet_none {
"and_then(<f>)"
} else if use_is_some_and_suggestion {
"is_some_and(<f>)"
} else {
"map_or(<a>, <f>)"
};
Expand All @@ -75,12 +94,18 @@ pub(super) fn check<'tcx>(
let mut suggestion = vec![
(
map_span,
String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
String::from(if unwrap_snippet_none {
"and_then"
} else if use_is_some_and_suggestion {
"is_some_and"
} else {
"map_or"
}),
),
(expr.span.with_lo(unwrap_recv.span.hi()), String::new()),
];

if !unwrap_snippet_none {
if !unwrap_snippet_none && !use_is_some_and_suggestion {
suggestion.push((map_arg_span.with_hi(map_arg_span.lo()), format!("{unwrap_snippet}, ")));
}

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ fn option_methods() {
.unwrap_or_else(||
0
);

// If the argument to unwrap_or is false, suggest is_some_and instead
let _ = opt.map(|x| x > 5).unwrap_or(false);
}

#[rustfmt::skip]
Expand Down
20 changes: 16 additions & 4 deletions tests/ui/map_unwrap_or.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ LL | | 0
LL | | );
| |_________^

error: called `map(<f>).unwrap_or(false)` on an `Option` value. This can be done more directly by calling `is_some_and(<f>)` instead
--> $DIR/map_unwrap_or.rs:61:13
|
LL | let _ = opt.map(|x| x > 5).unwrap_or(false);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use `is_some_and(<f>)` instead
|
LL - let _ = opt.map(|x| x > 5).unwrap_or(false);
LL + let _ = opt.is_some_and(|x| x > 5);
|

error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
--> $DIR/map_unwrap_or.rs:67:13
--> $DIR/map_unwrap_or.rs:70:13
|
LL | let _ = res.map(|x| {
| _____________^
Expand All @@ -137,7 +149,7 @@ LL | | ).unwrap_or_else(|_e| 0);
| |____________________________^

error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
--> $DIR/map_unwrap_or.rs:71:13
--> $DIR/map_unwrap_or.rs:74:13
|
LL | let _ = res.map(|x| x + 1)
| _____________^
Expand All @@ -147,10 +159,10 @@ LL | | });
| |__________^

error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
--> $DIR/map_unwrap_or.rs:95:13
--> $DIR/map_unwrap_or.rs:98:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`

error: aborting due to 12 previous errors
error: aborting due to 13 previous errors