@@ -370,26 +370,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
370
370
return ;
371
371
}
372
372
}
373
- let ( lint , msg ) = if is_named_constant ( cx, left) || is_named_constant ( cx, right) {
374
- ( FLOAT_CMP_CONST , "strict comparison of `f32` or `f64` constant" )
375
- } else {
376
- ( FLOAT_CMP , "strict comparison of `f32` or `f64`" )
377
- } ;
373
+ let is_comparing_arrays = is_array ( cx, left) || is_array ( cx, right) ;
374
+ let ( lint , msg ) = get_lint_and_message (
375
+ is_named_constant ( cx , left ) || is_named_constant ( cx , right ) ,
376
+ is_comparing_arrays ,
377
+ ) ;
378
378
span_lint_and_then ( cx, lint, expr. span , msg, |db| {
379
379
let lhs = Sugg :: hir ( cx, left, ".." ) ;
380
380
let rhs = Sugg :: hir ( cx, right, ".." ) ;
381
381
382
- db. span_suggestion (
383
- expr. span ,
384
- "consider comparing them within some error" ,
385
- format ! (
386
- "({}).abs() {} error" ,
387
- lhs - rhs,
388
- if op == BinOpKind :: Eq { '<' } else { '>' }
389
- ) ,
390
- Applicability :: HasPlaceholders , // snippet
391
- ) ;
392
- db. span_note ( expr. span , "`std::f32::EPSILON` and `std::f64::EPSILON` are available." ) ;
382
+ if !is_comparing_arrays {
383
+ db. span_suggestion (
384
+ expr. span ,
385
+ "consider comparing them within some error" ,
386
+ format ! (
387
+ "({}).abs() {} error" ,
388
+ lhs - rhs,
389
+ if op == BinOpKind :: Eq { '<' } else { '>' }
390
+ ) ,
391
+ Applicability :: HasPlaceholders , // snippet
392
+ ) ;
393
+ }
394
+ db. note ( "`std::f32::EPSILON` and `std::f64::EPSILON` are available for the `error`" ) ;
393
395
} ) ;
394
396
} else if op == BinOpKind :: Rem && is_integer_const ( cx, right, 1 ) {
395
397
span_lint ( cx, MODULO_ONE , expr. span , "any number modulo 1 will be 0" ) ;
@@ -441,6 +443,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
441
443
}
442
444
}
443
445
446
+ fn get_lint_and_message (
447
+ is_comparing_constants : bool ,
448
+ is_comparing_arrays : bool ,
449
+ ) -> ( & ' static rustc_lint:: Lint , & ' static str ) {
450
+ if is_comparing_constants {
451
+ (
452
+ FLOAT_CMP_CONST ,
453
+ if is_comparing_arrays {
454
+ "strict comparison of `f32` or `f64` constant arrays"
455
+ } else {
456
+ "strict comparison of `f32` or `f64` constant"
457
+ } ,
458
+ )
459
+ } else {
460
+ (
461
+ FLOAT_CMP ,
462
+ if is_comparing_arrays {
463
+ "strict comparison of `f32` or `f64` arrays"
464
+ } else {
465
+ "strict comparison of `f32` or `f64`"
466
+ } ,
467
+ )
468
+ }
469
+ }
470
+
444
471
fn check_nan ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , cmp_expr : & Expr < ' _ > ) {
445
472
if_chain ! {
446
473
if !in_constant( cx, cmp_expr. hir_id) ;
@@ -476,6 +503,11 @@ fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) -> boo
476
503
match constant ( cx, cx. tables , expr) {
477
504
Some ( ( Constant :: F32 ( f) , _) ) => f == 0.0 || f. is_infinite ( ) ,
478
505
Some ( ( Constant :: F64 ( f) , _) ) => f == 0.0 || f. is_infinite ( ) ,
506
+ Some ( ( Constant :: Vec ( vec) , _) ) => vec. iter ( ) . all ( |f| match f {
507
+ Constant :: F32 ( f) => * f == 0.0 || ( * f) . is_infinite ( ) ,
508
+ Constant :: F64 ( f) => * f == 0.0 || ( * f) . is_infinite ( ) ,
509
+ _ => false ,
510
+ } ) ,
479
511
_ => false ,
480
512
}
481
513
}
@@ -500,7 +532,17 @@ fn is_signum(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
500
532
}
501
533
502
534
fn is_float ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) -> bool {
503
- matches ! ( walk_ptrs_ty( cx. tables. expr_ty( expr) ) . kind, ty:: Float ( _) )
535
+ let value = & walk_ptrs_ty ( cx. tables . expr_ty ( expr) ) . kind ;
536
+
537
+ if let ty:: Array ( arr_ty, _) = value {
538
+ return matches ! ( arr_ty. kind, ty:: Float ( _) ) ;
539
+ } ;
540
+
541
+ matches ! ( value, ty:: Float ( _) )
542
+ }
543
+
544
+ fn is_array ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) -> bool {
545
+ matches ! ( & walk_ptrs_ty( cx. tables. expr_ty( expr) ) . kind, ty:: Array ( _, _) )
504
546
}
505
547
506
548
fn check_to_owned ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , other : & Expr < ' _ > ) {
0 commit comments