changed the msrv to 1.70 to suggest is_some_and

if the msrv is not >= 1.70 then the `map_or` is suggested instead of `is_some_and` (even when `unwrap_or` returns false)
This commit is contained in:
darklyspaced 2023-06-26 13:39:45 +08:00
parent 9b7d8d1dc7
commit c60222dc12
No known key found for this signature in database
5 changed files with 49 additions and 6 deletions

View file

@ -3906,7 +3906,7 @@ impl Methods {
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]);
},
Some(("map", m_recv, [m_arg], span, _)) => {
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span);
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span, &self.msrv);
},
Some(("then_some", t_recv, [t_arg], _, _)) => {
obfuscated_if_else::check(cx, expr, t_recv, t_arg, u_arg);

View file

@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_copy;
use clippy_utils::ty::is_type_diagnostic_item;
@ -27,6 +28,7 @@ pub(super) fn check<'tcx>(
unwrap_recv: &rustc_hir::Expr<'_>,
unwrap_arg: &'tcx rustc_hir::Expr<'_>,
map_span: Span,
msrv: &Msrv,
) {
// lint if the caller of `map()` is an `Option`
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) {
@ -75,10 +77,12 @@ pub(super) fn check<'tcx>(
}
let mut suggest_is_some_and = false;
// argument to `unwrap_or` is false; should suggest using `is_some_and`
if let ExprKind::Lit(unwrap_lit) = &unwrap_arg.kind {
if let rustc_ast::LitKind::Bool(false) = unwrap_lit.node {
suggest_is_some_and = true;
// argument to `unwrap_or` is false & is_some_and is stabilised; should suggest using `is_some_and`
if msrv.meets(msrvs::OPT_IS_SOME_AND) {
if let ExprKind::Lit(unwrap_lit) = &unwrap_arg.kind {
if let rustc_ast::LitKind::Bool(false) = unwrap_lit.node {
suggest_is_some_and = true;
}
}
}

View file

@ -19,6 +19,7 @@ macro_rules! msrv_aliases {
// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,70,0 { OPT_IS_SOME_AND }
1,68,0 { PATH_MAIN_SEPARATOR_STR }
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }

View file

@ -99,6 +99,20 @@ fn msrv_1_41() {
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
}
#[clippy::msrv = "1.69"]
fn msrv_1_69() {
let opt: Option<i32> = Some(1);
let _ = opt.map(|x| x > 5).unwrap_or(false);
}
#[clippy::msrv = "1.70"]
fn msrv_1_70() {
let opt: Option<i32> = Some(1);
let _ = opt.map(|x| x > 5).unwrap_or(false);
}
mod issue_10579 {
// Different variations of the same issue.
fn v1() {

View file

@ -164,5 +164,29 @@ error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be do
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 13 previous errors
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
--> $DIR/map_unwrap_or.rs:106:13
|
LL | let _ = opt.map(|x| x > 5).unwrap_or(false);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use `map_or(<a>, <f>)` instead
|
LL - let _ = opt.map(|x| x > 5).unwrap_or(false);
LL + let _ = opt.map_or(false, |x| x > 5);
|
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:113: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: aborting due to 15 previous errors