forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#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 rust-lang#758
- Loading branch information
Showing
6 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[deny(clippy::while_float)] | ||
fn main() { | ||
let mut x = 0.0_f32; | ||
while x < 42.0_f32 { | ||
x += 0.5; | ||
} | ||
while x < 42.0 { | ||
x += 1.0; | ||
} | ||
let mut x = 0; | ||
while x < 42 { | ||
x += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: while condition comparing floats | ||
--> tests/ui/while_float.rs:4:11 | ||
| | ||
LL | while x < 42.0_f32 { | ||
| ^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> tests/ui/while_float.rs:1:8 | ||
| | ||
LL | #[deny(clippy::while_float)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: while condition comparing floats | ||
--> tests/ui/while_float.rs:7:11 | ||
| | ||
LL | while x < 42.0 { | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|