From c9ea0ae502aa5afe84ae528b697824412ebabf77 Mon Sep 17 00:00:00 2001 From: Yusuf Raji Date: Sun, 5 May 2024 23:26:29 +0200 Subject: Lint while loops with float comparison --- clippy_lints/src/loops/while_float.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 clippy_lints/src/loops/while_float.rs (limited to 'clippy_lints/src/loops/while_float.rs') 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() +} -- cgit 1.4.1-3-g733a5