@@ -17,6 +17,7 @@ mod same_item_push;
1717mod single_element_loop;
1818mod unused_enumerate_index;
1919mod utils;
20+ mod while_float;
2021mod while_immutable_condition;
2122mod while_let_loop;
2223mod while_let_on_iterator;
@@ -416,6 +417,39 @@ declare_clippy_lint! {
416417 "variables used within while expression are not mutated in the body"
417418}
418419
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+
419453declare_clippy_lint ! {
420454 /// ### What it does
421455 /// Checks whether a for loop is being used to push a constant
@@ -706,6 +740,7 @@ impl_lint_pass!(Loops => [
706740 NEVER_LOOP ,
707741 MUT_RANGE_BOUND ,
708742 WHILE_IMMUTABLE_CONDITION ,
743+ WHILE_FLOAT ,
709744 SAME_ITEM_PUSH ,
710745 SINGLE_ELEMENT_LOOP ,
711746 MISSING_SPIN_LOOP ,
@@ -762,6 +797,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
762797
763798 if let Some ( higher:: While { condition, body, span } ) = higher:: While :: hir ( expr) {
764799 while_immutable_condition:: check ( cx, condition, body) ;
800+ while_float:: check ( cx, condition) ;
765801 missing_spin_loop:: check ( cx, condition, body) ;
766802 manual_while_let_some:: check ( cx, condition, body, span) ;
767803 }
0 commit comments