@@ -3,7 +3,7 @@ use clippy_config::Conf;
3
3
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_and_then} ;
4
4
use clippy_utils:: source:: snippet_opt;
5
5
use clippy_utils:: {
6
- higher, in_constant , is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq ,
6
+ higher, is_in_const_context , is_integer_literal, path_to_local, peel_blocks, peel_blocks_with_stmt, SpanlessEq ,
7
7
} ;
8
8
use rustc_ast:: ast:: LitKind ;
9
9
use rustc_data_structures:: packed:: Pu128 ;
@@ -94,9 +94,6 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
94
94
if expr. span . from_expansion ( ) {
95
95
return ;
96
96
}
97
- if in_constant ( cx, expr. hir_id ) && !self . msrv . meets ( msrvs:: SATURATING_SUB_CONST ) {
98
- return ;
99
- }
100
97
if let Some ( higher:: If { cond, then, r#else : None } ) = higher:: If :: hir ( expr)
101
98
102
99
// Check if the conditional expression is a binary operation
@@ -110,13 +107,16 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
110
107
} ) = higher:: If :: hir ( expr)
111
108
&& let ExprKind :: Binary ( ref cond_op, cond_left, cond_right) = cond. kind
112
109
{
113
- check_manual_check ( cx, expr, cond_op, cond_left, cond_right, if_block, else_block) ;
110
+ check_manual_check (
111
+ cx, expr, cond_op, cond_left, cond_right, if_block, else_block, & self . msrv ,
112
+ ) ;
114
113
}
115
114
}
116
115
117
116
extract_msrv_attr ! ( LateContext ) ;
118
117
}
119
118
119
+ #[ allow( clippy:: too_many_arguments) ]
120
120
fn check_manual_check < ' tcx > (
121
121
cx : & LateContext < ' tcx > ,
122
122
expr : & Expr < ' tcx > ,
@@ -125,6 +125,7 @@ fn check_manual_check<'tcx>(
125
125
right_hand : & Expr < ' tcx > ,
126
126
if_block : & Expr < ' tcx > ,
127
127
else_block : & Expr < ' tcx > ,
128
+ msrv : & Msrv ,
128
129
) {
129
130
let ty = cx. typeck_results ( ) . expr_ty ( left_hand) ;
130
131
if ty. is_numeric ( ) && !ty. is_signed ( ) {
@@ -137,6 +138,7 @@ fn check_manual_check<'tcx>(
137
138
right_hand,
138
139
if_block,
139
140
else_block,
141
+ msrv,
140
142
) ,
141
143
BinOpKind :: Lt | BinOpKind :: Le => check_gt (
142
144
cx,
@@ -146,12 +148,14 @@ fn check_manual_check<'tcx>(
146
148
left_hand,
147
149
if_block,
148
150
else_block,
151
+ msrv,
149
152
) ,
150
153
_ => { } ,
151
154
}
152
155
}
153
156
}
154
157
158
+ #[ allow( clippy:: too_many_arguments) ]
155
159
fn check_gt (
156
160
cx : & LateContext < ' _ > ,
157
161
condition_span : Span ,
@@ -160,11 +164,21 @@ fn check_gt(
160
164
little_var : & Expr < ' _ > ,
161
165
if_block : & Expr < ' _ > ,
162
166
else_block : & Expr < ' _ > ,
167
+ msrv : & Msrv ,
163
168
) {
164
169
if let Some ( big_var) = Var :: new ( big_var)
165
170
&& let Some ( little_var) = Var :: new ( little_var)
166
171
{
167
- check_subtraction ( cx, condition_span, expr_span, big_var, little_var, if_block, else_block) ;
172
+ check_subtraction (
173
+ cx,
174
+ condition_span,
175
+ expr_span,
176
+ big_var,
177
+ little_var,
178
+ if_block,
179
+ else_block,
180
+ msrv,
181
+ ) ;
168
182
}
169
183
}
170
184
@@ -182,6 +196,7 @@ impl Var {
182
196
}
183
197
}
184
198
199
+ #[ allow( clippy:: too_many_arguments) ]
185
200
fn check_subtraction (
186
201
cx : & LateContext < ' _ > ,
187
202
condition_span : Span ,
@@ -190,6 +205,7 @@ fn check_subtraction(
190
205
little_var : Var ,
191
206
if_block : & Expr < ' _ > ,
192
207
else_block : & Expr < ' _ > ,
208
+ msrv : & Msrv ,
193
209
) {
194
210
let if_block = peel_blocks ( if_block) ;
195
211
let else_block = peel_blocks ( else_block) ;
@@ -201,7 +217,16 @@ fn check_subtraction(
201
217
}
202
218
// If the subtraction is done in the `else` block, then we need to also revert the two
203
219
// variables as it means that the check was reverted too.
204
- check_subtraction ( cx, condition_span, expr_span, little_var, big_var, else_block, if_block) ;
220
+ check_subtraction (
221
+ cx,
222
+ condition_span,
223
+ expr_span,
224
+ little_var,
225
+ big_var,
226
+ else_block,
227
+ if_block,
228
+ msrv,
229
+ ) ;
205
230
return ;
206
231
}
207
232
if is_integer_literal ( else_block, 0 )
@@ -215,6 +240,7 @@ fn check_subtraction(
215
240
// if `snippet_opt` fails, it won't try the next conditions.
216
241
if let Some ( big_var_snippet) = snippet_opt ( cx, big_var. span )
217
242
&& let Some ( little_var_snippet) = snippet_opt ( cx, little_var. span )
243
+ && ( !is_in_const_context ( cx) || msrv. meets ( msrvs:: SATURATING_SUB_CONST ) )
218
244
{
219
245
span_lint_and_sugg (
220
246
cx,
0 commit comments