@@ -2,7 +2,7 @@ use clippy_config::msrvs::{self, Msrv};
2
2
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
3
use clippy_utils:: eager_or_lazy:: switch_to_eager_eval;
4
4
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 ;
6
6
use clippy_utils:: visitors:: is_local_used;
7
7
use clippy_utils:: { get_parent_expr, is_from_proc_macro, path_to_local_id} ;
8
8
use rustc_ast:: LitKind :: Bool ;
@@ -42,27 +42,20 @@ pub(super) fn check<'a>(
42
42
map : & Expr < ' _ > ,
43
43
msrv : & Msrv ,
44
44
) {
45
- if is_from_proc_macro ( cx, expr) {
46
- return ;
47
- }
48
-
49
45
let ExprKind :: Lit ( def_kind) = def. kind else {
50
46
return ;
51
47
} ;
52
48
53
49
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
- }
57
50
58
51
let Bool ( def_bool) = def_kind. node else {
59
52
return ;
60
53
} ;
61
54
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 ,
66
59
} ;
67
60
68
61
let ( sugg, method) = if let ExprKind :: Closure ( map_closure) = map. kind
@@ -94,6 +87,7 @@ pub(super) fn check<'a>(
94
87
// since for example `Some(5).map_or(false, |x| x == 5).then(|| 1)`
95
88
// being converted to `Some(5) == Some(5).then(|| 1)` isnt
96
89
// the same thing
90
+
97
91
let should_add_parens = get_parent_expr ( cx, expr)
98
92
. is_some_and ( |expr| expr. precedence ( ) . order ( ) > i8:: try_from ( AssocOp :: Equal . precedence ( ) ) . unwrap_or ( 0 ) ) ;
99
93
(
@@ -104,7 +98,7 @@ pub(super) fn check<'a>(
104
98
snippet( cx, non_binding_location. span. source_callsite( ) , ".." ) ,
105
99
if should_add_parens { ")" } else { "" }
106
100
) ,
107
- "standard comparison" ,
101
+ "a standard comparison" ,
108
102
)
109
103
} else if !def_bool
110
104
&& msrv. meets ( msrvs:: OPTION_RESULT_IS_VARIANT_AND )
@@ -120,13 +114,17 @@ pub(super) fn check<'a>(
120
114
return ;
121
115
} ;
122
116
117
+ if is_from_proc_macro ( cx, expr) {
118
+ return ;
119
+ }
120
+
123
121
span_lint_and_sugg (
124
122
cx,
125
123
UNNECESSARY_MAP_OR ,
126
124
expr. span ,
127
125
"this `map_or` is redundant" ,
128
126
format ! ( "use {method} instead" ) ,
129
127
sugg,
130
- Applicability :: MachineApplicable ,
128
+ Applicability :: MaybeIncorrect ,
131
129
) ;
132
130
}
0 commit comments