@@ -207,7 +207,13 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
207207 extract_msrv_attr ! ( LateContext ) ;
208208}
209209
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+ ) {
211217 if in_constant ( cx, expr. hir_id ) {
212218 return ;
213219 }
@@ -219,21 +225,19 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
219225 _ => return ,
220226 } ;
221227 // 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) ) {
225229 // we only lint comparisons on the same name and with different
226230 // direction
227- if lid != rid || lord == rord {
231+ if l . id != r . id || l . ord == r . ord {
228232 return ;
229233 }
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 ) {
232236 // 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 )
235239 } else {
236- ( rval_span , lval_span , rinc , linc )
240+ ( r . val_span , l . val_span , r . inc , l . inc )
237241 } ;
238242 // we only lint inclusive lower bounds
239243 if !l_inc {
@@ -245,7 +249,7 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
245249 ( "Range" , ".." )
246250 } ;
247251 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) ;
249253 let lo = snippet_with_applicability ( cx, l_span, "_" , & mut applicability) ;
250254 let hi = snippet_with_applicability ( cx, u_span, "_" , & mut applicability) ;
251255 let space = if lo. ends_with ( '.' ) { " " } else { "" } ;
@@ -258,13 +262,13 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
258262 format ! ( "({}{}{}{}).contains(&{})" , lo, space, range_op, hi, name) ,
259263 applicability,
260264 ) ;
261- } else if !combine_and && ord == Some ( lord ) {
265+ } else if !combine_and && ord == Some ( l . ord ) {
262266 // `!_.contains(_)`
263267 // 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 )
266270 } else {
267- ( rval_span , lval_span , rinc , linc )
271+ ( r . val_span , l . val_span , r . inc , l . inc )
268272 } ;
269273 if l_inc {
270274 return ;
@@ -275,7 +279,7 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
275279 ( "RangeInclusive" , "..=" )
276280 } ;
277281 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) ;
279283 let lo = snippet_with_applicability ( cx, l_span, "_" , & mut applicability) ;
280284 let hi = snippet_with_applicability ( cx, u_span, "_" , & mut applicability) ;
281285 let space = if lo. ends_with ( '.' ) { " " } else { "" } ;
@@ -292,7 +296,20 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
292296 }
293297}
294298
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 > > {
296313 if let ExprKind :: Binary ( ref op, l, r) = ex. kind {
297314 let ( inclusive, ordering) = match op. node {
298315 BinOpKind :: Gt => ( false , Ordering :: Greater ) ,
@@ -303,11 +320,27 @@ fn check_range_bounds(cx: &LateContext<'_>, ex: &Expr<'_>) -> Option<(Constant,
303320 } ;
304321 if let Some ( id) = path_to_local ( l) {
305322 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+ } ) ;
307332 }
308333 } else if let Some ( id) = path_to_local ( r) {
309334 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+ } ) ;
311344 }
312345 }
313346 }
0 commit comments