Skip to content

Commit 9d2e7f3

Browse files
committed
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)
1 parent a48bc5b commit 9d2e7f3

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3906,7 +3906,7 @@ impl Methods {
39063906
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]);
39073907
},
39083908
Some(("map", m_recv, [m_arg], span, _)) => {
3909-
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span);
3909+
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span, &self.msrv);
39103910
},
39113911
Some(("then_some", t_recv, [t_arg], _, _)) => {
39123912
obfuscated_if_else::check(cx, expr, t_recv, t_arg, u_arg);

clippy_lints/src/methods/option_map_unwrap_or.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::source::snippet_with_applicability;
34
use clippy_utils::ty::is_copy;
45
use clippy_utils::ty::is_type_diagnostic_item;
@@ -27,6 +28,7 @@ pub(super) fn check<'tcx>(
2728
unwrap_recv: &rustc_hir::Expr<'_>,
2829
unwrap_arg: &'tcx rustc_hir::Expr<'_>,
2930
map_span: Span,
31+
msrv: &Msrv,
3032
) {
3133
// lint if the caller of `map()` is an `Option`
3234
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) {
@@ -75,10 +77,12 @@ pub(super) fn check<'tcx>(
7577
}
7678

7779
let mut suggest_is_some_and = false;
78-
// argument to `unwrap_or` is false; should suggest using `is_some_and`
79-
if let ExprKind::Lit(unwrap_lit) = &unwrap_arg.kind {
80-
if let rustc_ast::LitKind::Bool(false) = unwrap_lit.node {
81-
suggest_is_some_and = true;
80+
// argument to `unwrap_or` is false & is_some_and is stabilised; should suggest using `is_some_and`
81+
if msrv.meets(msrvs::OPT_IS_SOME_AND) {
82+
if let ExprKind::Lit(unwrap_lit) = &unwrap_arg.kind {
83+
if let rustc_ast::LitKind::Bool(false) = unwrap_lit.node {
84+
suggest_is_some_and = true;
85+
}
8286
}
8387
}
8488

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ macro_rules! msrv_aliases {
1919

2020
// names may refer to stabilized feature flags or library items
2121
msrv_aliases! {
22+
1,70,0 { OPT_IS_SOME_AND }
2223
1,68,0 { PATH_MAIN_SEPARATOR_STR }
2324
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
2425
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }

tests/ui/map_unwrap_or.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ fn msrv_1_41() {
9999
let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
100100
}
101101

102+
#[clippy::msrv = "1.69"]
103+
fn msrv_1_69() {
104+
let opt: Option<i32> = Some(1);
105+
106+
let _ = opt.map(|x| x > 5).unwrap_or(false);
107+
}
108+
109+
#[clippy::msrv = "1.70"]
110+
fn msrv_1_70() {
111+
let opt: Option<i32> = Some(1);
112+
113+
let _ = opt.map(|x| x > 5).unwrap_or(false);
114+
}
115+
102116
mod issue_10579 {
103117
// Different variations of the same issue.
104118
fn v1() {

tests/ui/map_unwrap_or.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,29 @@ error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be do
164164
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
165165
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
166166

167-
error: aborting due to 13 previous errors
167+
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
168+
--> $DIR/map_unwrap_or.rs:106:13
169+
|
170+
LL | let _ = opt.map(|x| x > 5).unwrap_or(false);
171+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
172+
|
173+
help: use `map_or(<a>, <f>)` instead
174+
|
175+
LL - let _ = opt.map(|x| x > 5).unwrap_or(false);
176+
LL + let _ = opt.map_or(false, |x| x > 5);
177+
|
178+
179+
error: called `map(<f>).unwrap_or(false)` on an `Option` value. This can be done more directly by calling `is_some_and(<f>)` instead
180+
--> $DIR/map_unwrap_or.rs:113:13
181+
|
182+
LL | let _ = opt.map(|x| x > 5).unwrap_or(false);
183+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
184+
|
185+
help: use `is_some_and(<f>)` instead
186+
|
187+
LL - let _ = opt.map(|x| x > 5).unwrap_or(false);
188+
LL + let _ = opt.is_some_and(|x| x > 5);
189+
|
190+
191+
error: aborting due to 15 previous errors
168192

0 commit comments

Comments
 (0)