@@ -207,7 +207,13 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
207
207
extract_msrv_attr ! ( LateContext ) ;
208
208
}
209
209
210
- fn check_possible_range_contains ( cx : & LateContext < ' _ > , op : BinOpKind , l : & Expr < ' _ > , r : & Expr < ' _ > , expr : & Expr < ' _ > ) {
210
+ fn check_possible_range_contains (
211
+ cx : & LateContext < ' _ > ,
212
+ op : BinOpKind ,
213
+ left : & Expr < ' _ > ,
214
+ right : & Expr < ' _ > ,
215
+ expr : & Expr < ' _ > ,
216
+ ) {
211
217
if in_constant ( cx, expr. hir_id ) {
212
218
return ;
213
219
}
@@ -219,21 +225,19 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
219
225
_ => return ,
220
226
} ;
221
227
// value, name, order (higher/lower), inclusiveness
222
- if let ( Some ( ( lval, lid, name_span, lval_span, lord, linc) ) , Some ( ( rval, rid, _, rval_span, rord, rinc) ) ) =
223
- ( check_range_bounds ( cx, l) , check_range_bounds ( cx, r) )
224
- {
228
+ if let ( Some ( l) , Some ( r) ) = ( check_range_bounds ( cx, left) , check_range_bounds ( cx, right) ) {
225
229
// we only lint comparisons on the same name and with different
226
230
// direction
227
- if lid != rid || lord == rord {
231
+ if l . id != r . id || l . ord == r . ord {
228
232
return ;
229
233
}
230
- let ord = Constant :: partial_cmp ( cx. tcx , cx. typeck_results ( ) . expr_ty ( l) , & lval , & rval ) ;
231
- if combine_and && ord == Some ( rord ) {
234
+ let ord = Constant :: partial_cmp ( cx. tcx , cx. typeck_results ( ) . expr_ty ( l. expr ) , & l . val , & r . val ) ;
235
+ if combine_and && ord == Some ( r . ord ) {
232
236
// order lower bound and upper bound
233
- let ( l_span, u_span, l_inc, u_inc) = if rord == Ordering :: Less {
234
- ( lval_span , rval_span , linc , rinc )
237
+ let ( l_span, u_span, l_inc, u_inc) = if r . ord == Ordering :: Less {
238
+ ( l . val_span , r . val_span , l . inc , r . inc )
235
239
} else {
236
- ( rval_span , lval_span , rinc , linc )
240
+ ( r . val_span , l . val_span , r . inc , l . inc )
237
241
} ;
238
242
// we only lint inclusive lower bounds
239
243
if !l_inc {
@@ -245,7 +249,7 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
245
249
( "Range" , ".." )
246
250
} ;
247
251
let mut applicability = Applicability :: MachineApplicable ;
248
- let name = snippet_with_applicability ( cx, name_span, "_" , & mut applicability) ;
252
+ let name = snippet_with_applicability ( cx, l . name_span , "_" , & mut applicability) ;
249
253
let lo = snippet_with_applicability ( cx, l_span, "_" , & mut applicability) ;
250
254
let hi = snippet_with_applicability ( cx, u_span, "_" , & mut applicability) ;
251
255
let space = if lo. ends_with ( '.' ) { " " } else { "" } ;
@@ -258,13 +262,13 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
258
262
format ! ( "({}{}{}{}).contains(&{})" , lo, space, range_op, hi, name) ,
259
263
applicability,
260
264
) ;
261
- } else if !combine_and && ord == Some ( lord ) {
265
+ } else if !combine_and && ord == Some ( l . ord ) {
262
266
// `!_.contains(_)`
263
267
// order lower bound and upper bound
264
- let ( l_span, u_span, l_inc, u_inc) = if lord == Ordering :: Less {
265
- ( lval_span , rval_span , linc , rinc )
268
+ let ( l_span, u_span, l_inc, u_inc) = if l . ord == Ordering :: Less {
269
+ ( l . val_span , r . val_span , l . inc , r . inc )
266
270
} else {
267
- ( rval_span , lval_span , rinc , linc )
271
+ ( r . val_span , l . val_span , r . inc , l . inc )
268
272
} ;
269
273
if l_inc {
270
274
return ;
@@ -275,7 +279,7 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
275
279
( "RangeInclusive" , "..=" )
276
280
} ;
277
281
let mut applicability = Applicability :: MachineApplicable ;
278
- let name = snippet_with_applicability ( cx, name_span, "_" , & mut applicability) ;
282
+ let name = snippet_with_applicability ( cx, l . name_span , "_" , & mut applicability) ;
279
283
let lo = snippet_with_applicability ( cx, l_span, "_" , & mut applicability) ;
280
284
let hi = snippet_with_applicability ( cx, u_span, "_" , & mut applicability) ;
281
285
let space = if lo. ends_with ( '.' ) { " " } else { "" } ;
@@ -292,7 +296,20 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
292
296
}
293
297
}
294
298
295
- fn check_range_bounds ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > ) -> Option < ( Constant , HirId , Span , Span , Ordering , bool ) > {
299
+ struct RangeBounds < ' a > {
300
+ val : Constant ,
301
+ expr : & ' a Expr < ' a > ,
302
+ id : HirId ,
303
+ name_span : Span ,
304
+ val_span : Span ,
305
+ ord : Ordering ,
306
+ inc : bool ,
307
+ }
308
+
309
+ // Takes a binary expression such as x <= 2 as input
310
+ // Breaks apart into various pieces, such as the value of the number,
311
+ // hir id of the variable, and direction/inclusiveness of the operator
312
+ fn check_range_bounds < ' a > ( cx : & ' a LateContext < ' _ > , ex : & ' a Expr < ' _ > ) -> Option < RangeBounds < ' a > > {
296
313
if let ExprKind :: Binary ( ref op, l, r) = ex. kind {
297
314
let ( inclusive, ordering) = match op. node {
298
315
BinOpKind :: Gt => ( false , Ordering :: Greater ) ,
@@ -303,11 +320,27 @@ fn check_range_bounds(cx: &LateContext<'_>, ex: &Expr<'_>) -> Option<(Constant,
303
320
} ;
304
321
if let Some ( id) = path_to_local ( l) {
305
322
if let Some ( ( c, _) ) = constant ( cx, cx. typeck_results ( ) , r) {
306
- return Some ( ( c, id, l. span , r. span , ordering, inclusive) ) ;
323
+ return Some ( RangeBounds {
324
+ val : c,
325
+ expr : r,
326
+ id,
327
+ name_span : l. span ,
328
+ val_span : r. span ,
329
+ ord : ordering,
330
+ inc : inclusive,
331
+ } ) ;
307
332
}
308
333
} else if let Some ( id) = path_to_local ( r) {
309
334
if let Some ( ( c, _) ) = constant ( cx, cx. typeck_results ( ) , l) {
310
- return Some ( ( c, id, r. span , l. span , ordering. reverse ( ) , inclusive) ) ;
335
+ return Some ( RangeBounds {
336
+ val : c,
337
+ expr : l,
338
+ id,
339
+ name_span : r. span ,
340
+ val_span : l. span ,
341
+ ord : ordering. reverse ( ) ,
342
+ inc : inclusive,
343
+ } ) ;
311
344
}
312
345
}
313
346
}
0 commit comments