about summary refs log tree commit diff
path: root/clippy_lints/src/loops/while_float.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-05-21 11:36:31 +0000
committerbors <bors@rust-lang.org>2024-05-21 11:36:31 +0000
commit2efebd2f0c03dabbe5c3ad7b4ebfbd99238d1fb2 (patch)
treecadf470d2c114545c2b5c44a89ac0c73f89478b3 /clippy_lints/src/loops/while_float.rs
parent0b1bf37722519e78d92c01118fde228e7ea9eb17 (diff)
parentcb3fcbbcfe3d92ae822f5fba402dbb3d48f30470 (diff)
Auto merge of #12765 - yusufraji:while-float, r=llogiq
Add new lint `while_float`

This PR adds a nursery lint that checks for while loops comparing floating point values.

changelog:
```
changelog: [`while_float`]: Checks for while loops comparing floating point values.
```

Fixes #758
Diffstat (limited to 'clippy_lints/src/loops/while_float.rs')
-rw-r--r--clippy_lints/src/loops/while_float.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/clippy_lints/src/loops/while_float.rs b/clippy_lints/src/loops/while_float.rs
new file mode 100644
index 00000000000..cf62ce29f0c
--- /dev/null
+++ b/clippy_lints/src/loops/while_float.rs
@@ -0,0 +1,20 @@
+use clippy_utils::diagnostics::span_lint;
+use rustc_hir::ExprKind;
+
+pub(super) fn check(cx: &rustc_lint::LateContext<'_>, condition: &rustc_hir::Expr<'_>) {
+    if let ExprKind::Binary(_op, left, right) = condition.kind
+        && is_float_type(cx, left)
+        && is_float_type(cx, right)
+    {
+        span_lint(
+            cx,
+            super::WHILE_FLOAT,
+            condition.span,
+            "while condition comparing floats",
+        );
+    }
+}
+
+fn is_float_type(cx: &rustc_lint::LateContext<'_>, expr: &rustc_hir::Expr<'_>) -> bool {
+    cx.typeck_results().expr_ty(expr).is_floating_point()
+}