@@ -2,7 +2,7 @@ use clippy_config::msrvs::{self, Msrv};
22use clippy_utils:: diagnostics:: span_lint_and_sugg;
33use clippy_utils:: eager_or_lazy:: switch_to_eager_eval;
44use clippy_utils:: source:: { snippet, snippet_opt} ;
5- use clippy_utils:: ty:: is_type_diagnostic_item ;
5+ use clippy_utils:: ty:: get_type_diagnostic_name ;
66use clippy_utils:: visitors:: is_local_used;
77use clippy_utils:: { get_parent_expr, is_from_proc_macro, path_to_local_id} ;
88use 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}
0 commit comments