@@ -17,6 +17,7 @@ mod same_item_push;
17
17
mod single_element_loop;
18
18
mod unused_enumerate_index;
19
19
mod utils;
20
+ mod while_float;
20
21
mod while_immutable_condition;
21
22
mod while_let_loop;
22
23
mod while_let_on_iterator;
@@ -416,6 +417,39 @@ declare_clippy_lint! {
416
417
"variables used within while expression are not mutated in the body"
417
418
}
418
419
420
+ declare_clippy_lint ! {
421
+ /// ### What it does
422
+ /// Checks for while loops comparing floating point values.
423
+ ///
424
+ /// ### Why is this bad?
425
+ /// If you increment floating point values, errors can compound,
426
+ /// so, use integers instead if possible.
427
+ ///
428
+ /// ### Known problems
429
+ /// The lint will catch all while loops comparing floating point
430
+ /// values no matter whether it's a threshold or something.
431
+ ///
432
+ /// ### Example
433
+ /// ```no_run
434
+ /// let mut x = 0.0;
435
+ /// while x < 42.0 {
436
+ /// x += 1.0;
437
+ /// }
438
+ /// ```
439
+ ///
440
+ /// Use instead:
441
+ /// ```no_run
442
+ /// let mut x = 0;
443
+ /// while x < 42 {
444
+ /// x += 1;
445
+ /// }
446
+ /// ```
447
+ #[ clippy:: version = "1.80.0" ]
448
+ pub WHILE_FLOAT ,
449
+ nursery,
450
+ "while loops comaparing floating point values"
451
+ }
452
+
419
453
declare_clippy_lint ! {
420
454
/// ### What it does
421
455
/// Checks whether a for loop is being used to push a constant
@@ -706,6 +740,7 @@ impl_lint_pass!(Loops => [
706
740
NEVER_LOOP ,
707
741
MUT_RANGE_BOUND ,
708
742
WHILE_IMMUTABLE_CONDITION ,
743
+ WHILE_FLOAT ,
709
744
SAME_ITEM_PUSH ,
710
745
SINGLE_ELEMENT_LOOP ,
711
746
MISSING_SPIN_LOOP ,
@@ -762,6 +797,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
762
797
763
798
if let Some ( higher:: While { condition, body, span } ) = higher:: While :: hir ( expr) {
764
799
while_immutable_condition:: check ( cx, condition, body) ;
800
+ while_float:: check ( cx, condition) ;
765
801
missing_spin_loop:: check ( cx, condition, body) ;
766
802
manual_while_let_some:: check ( cx, condition, body, span) ;
767
803
}
0 commit comments