@@ -2,14 +2,14 @@ use clippy_utils::sugg::Sugg;
2
2
use rustc_errors:: Applicability ;
3
3
use rustc_hir:: def:: Res ;
4
4
use rustc_hir:: { Arm , Expr , ExprKind , HirId , LangItem , MatchSource , Pat , PatKind , QPath } ;
5
- use rustc_lint:: { LateContext , LateLintPass } ;
5
+ use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
6
6
use rustc_session:: declare_lint_pass;
7
7
use rustc_span:: sym;
8
8
9
9
use clippy_utils:: diagnostics:: span_lint_and_sugg;
10
10
use clippy_utils:: source:: snippet_opt;
11
11
use clippy_utils:: ty:: implements_trait;
12
- use clippy_utils:: { in_constant, is_default_equivalent} ;
12
+ use clippy_utils:: { in_constant, is_default_equivalent, peel_blocks , span_contains_comment } ;
13
13
14
14
declare_clippy_lint ! {
15
15
/// ### What it does
@@ -119,22 +119,27 @@ fn handle_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
119
119
// We now get the bodies for both the `Some` and `None` arms.
120
120
&& let Some ( ( ( body_some, binding_id) , body_none) ) = get_some_and_none_bodies ( cx, arm1, arm2)
121
121
// We check that the `Some(x) => x` doesn't do anything apart "returning" the value in `Some`.
122
- && let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = body_some . peel_blocks ( ) . kind
122
+ && let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = peel_blocks ( body_some ) . kind
123
123
&& let Res :: Local ( local_id) = path. res
124
124
&& local_id == binding_id
125
125
// We now check the `None` arm is calling a method equivalent to `Default::default`.
126
- && let body_none = body_none . peel_blocks ( )
126
+ && let body_none = peel_blocks ( body_none )
127
127
&& is_default_equivalent ( cx, body_none)
128
128
&& let Some ( receiver) = Sugg :: hir_opt ( cx, match_expr) . map ( Sugg :: maybe_par)
129
129
{
130
+ let applicability = if span_contains_comment ( cx. sess ( ) . source_map ( ) , expr. span ) {
131
+ Applicability :: MaybeIncorrect
132
+ } else {
133
+ Applicability :: MachineApplicable
134
+ } ;
130
135
span_lint_and_sugg (
131
136
cx,
132
137
MANUAL_UNWRAP_OR_DEFAULT ,
133
138
expr. span ,
134
139
"match can be simplified with `.unwrap_or_default()`" ,
135
140
"replace it with" ,
136
141
format ! ( "{receiver}.unwrap_or_default()" ) ,
137
- Applicability :: MachineApplicable ,
142
+ applicability ,
138
143
) ;
139
144
}
140
145
true
@@ -150,22 +155,27 @@ fn handle_if_let<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
150
155
&& implements_trait ( cx, match_ty, default_trait_id, & [ ] )
151
156
&& let Some ( binding_id) = get_some ( cx, let_. pat )
152
157
// We check that the `Some(x) => x` doesn't do anything apart "returning" the value in `Some`.
153
- && let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = if_block . peel_blocks ( ) . kind
158
+ && let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = peel_blocks ( if_block ) . kind
154
159
&& let Res :: Local ( local_id) = path. res
155
160
&& local_id == binding_id
156
161
// We now check the `None` arm is calling a method equivalent to `Default::default`.
157
- && let body_else = else_expr . peel_blocks ( )
162
+ && let body_else = peel_blocks ( else_expr )
158
163
&& is_default_equivalent ( cx, body_else)
159
164
&& let Some ( if_let_expr_snippet) = snippet_opt ( cx, let_. init . span )
160
165
{
166
+ let applicability = if span_contains_comment ( cx. sess ( ) . source_map ( ) , expr. span ) {
167
+ Applicability :: MaybeIncorrect
168
+ } else {
169
+ Applicability :: MachineApplicable
170
+ } ;
161
171
span_lint_and_sugg (
162
172
cx,
163
173
MANUAL_UNWRAP_OR_DEFAULT ,
164
174
expr. span ,
165
175
"if let can be simplified with `.unwrap_or_default()`" ,
166
176
"replace it with" ,
167
177
format ! ( "{if_let_expr_snippet}.unwrap_or_default()" ) ,
168
- Applicability :: MachineApplicable ,
178
+ applicability ,
169
179
) ;
170
180
}
171
181
}
0 commit comments