Skip to content

Commit 9896c1c

Browse files
committed
refactor and move proc macro check
1 parent f774d4e commit 9896c1c

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

clippy_lints/src/methods/unnecessary_map_or.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
44
use clippy_utils::source::{snippet, snippet_opt};
5-
use clippy_utils::ty::is_type_diagnostic_item;
5+
use clippy_utils::ty::get_type_diagnostic_name;
66
use clippy_utils::visitors::is_local_used;
77
use clippy_utils::{get_parent_expr, is_from_proc_macro, path_to_local_id};
88
use rustc_ast::LitKind::Bool;
@@ -42,27 +42,20 @@ pub(super) fn check<'a>(
4242
map: &Expr<'_>,
4343
msrv: &Msrv,
4444
) {
45-
if is_from_proc_macro(cx, expr) {
46-
return;
47-
}
48-
4945
let ExprKind::Lit(def_kind) = def.kind else {
5046
return;
5147
};
5248

5349
let recv_ty = cx.typeck_results().expr_ty(recv);
54-
if !(is_type_diagnostic_item(cx, recv_ty, sym::Option) || is_type_diagnostic_item(cx, recv_ty, sym::Result)) {
55-
return;
56-
}
5750

5851
let Bool(def_bool) = def_kind.node else {
5952
return;
6053
};
6154

62-
let variant = if is_type_diagnostic_item(cx, recv_ty, sym::Option) {
63-
Variant::Some
64-
} else {
65-
Variant::Ok
55+
let variant = match get_type_diagnostic_name(cx, recv_ty) {
56+
Some(sym::Option) => Variant::Some,
57+
Some(sym::Result) => Variant::Ok,
58+
Some(_) | None => return,
6659
};
6760

6861
let (sugg, method) = if let ExprKind::Closure(map_closure) = map.kind
@@ -94,6 +87,7 @@ pub(super) fn check<'a>(
9487
// since for example `Some(5).map_or(false, |x| x == 5).then(|| 1)`
9588
// being converted to `Some(5) == Some(5).then(|| 1)` isnt
9689
// the same thing
90+
9791
let should_add_parens = get_parent_expr(cx, expr)
9892
.is_some_and(|expr| expr.precedence().order() > i8::try_from(AssocOp::Equal.precedence()).unwrap_or(0));
9993
(
@@ -104,7 +98,7 @@ pub(super) fn check<'a>(
10498
snippet(cx, non_binding_location.span.source_callsite(), ".."),
10599
if should_add_parens { ")" } else { "" }
106100
),
107-
"standard comparison",
101+
"a standard comparison",
108102
)
109103
} else if !def_bool
110104
&& msrv.meets(msrvs::OPTION_RESULT_IS_VARIANT_AND)
@@ -120,13 +114,17 @@ pub(super) fn check<'a>(
120114
return;
121115
};
122116

117+
if is_from_proc_macro(cx, expr) {
118+
return;
119+
}
120+
123121
span_lint_and_sugg(
124122
cx,
125123
UNNECESSARY_MAP_OR,
126124
expr.span,
127125
"this `map_or` is redundant",
128126
format!("use {method} instead"),
129127
sugg,
130-
Applicability::MachineApplicable,
128+
Applicability::MaybeIncorrect,
131129
);
132130
}

tests/ui/unnecessary_map_or.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: this `map_or` is redundant
22
--> tests/ui/unnecessary_map_or.rs:12:13
33
|
44
LL | let _ = Some(5).map_or(false, |n| n == 5);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use standard comparison instead: `Some(5) == Some(5)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Some(5) == Some(5)`
66
|
77
= note: `-D clippy::unnecessary-map-or` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_map_or)]`
@@ -11,7 +11,7 @@ error: this `map_or` is redundant
1111
--> tests/ui/unnecessary_map_or.rs:13:13
1212
|
1313
LL | let _ = Some(5).map_or(true, |n| n != 5);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use standard comparison instead: `Some(5) != Some(5)`
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Some(5) != Some(5)`
1515

1616
error: this `map_or` is redundant
1717
--> tests/ui/unnecessary_map_or.rs:14:13
@@ -21,7 +21,7 @@ LL | let _ = Some(5).map_or(false, |n| {
2121
LL | | let _ = 1;
2222
LL | | n == 5
2323
LL | | });
24-
| |______^ help: use standard comparison instead: `Some(5) == Some(5)`
24+
| |______^ help: use a standard comparison instead: `Some(5) == Some(5)`
2525

2626
error: this `map_or` is redundant
2727
--> tests/ui/unnecessary_map_or.rs:18:13
@@ -75,13 +75,13 @@ error: this `map_or` is redundant
7575
--> tests/ui/unnecessary_map_or.rs:27:13
7676
|
7777
LL | let _ = Ok::<i32, i32>(5).map_or(false, |n| n == 5);
78-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use standard comparison instead: `Ok::<i32, i32>(5) == Ok(5)`
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Ok::<i32, i32>(5) == Ok(5)`
7979

8080
error: this `map_or` is redundant
8181
--> tests/ui/unnecessary_map_or.rs:28:13
8282
|
8383
LL | let _ = Some(5).map_or(false, |n| n == 5).then(|| 1);
84-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use standard comparison instead: `(Some(5) == Some(5))`
84+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `(Some(5) == Some(5))`
8585

8686
error: aborting due to 11 previous errors
8787

0 commit comments

Comments
 (0)