-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #9617 - llogiq:cast-nan-to-int, r=Alexendoo
add `cast-nan-to-int` lint This fixes #371. r? `@Alexendoo` --- changelog: add [`cast-nan-to-int`] lint
- Loading branch information
Showing
10 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::CAST_NAN_TO_INT; | ||
|
||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::diagnostics::span_lint_and_note; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::Ty; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, from_ty: Ty<'_>, to_ty: Ty<'_>) { | ||
if from_ty.is_floating_point() && to_ty.is_integral() && is_known_nan(cx, cast_expr) { | ||
span_lint_and_note( | ||
cx, | ||
CAST_NAN_TO_INT, | ||
expr.span, | ||
&format!("casting a known NaN to {to_ty}"), | ||
None, | ||
"this always evaluates to 0", | ||
); | ||
} | ||
} | ||
|
||
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool { | ||
match constant(cx, cx.typeck_results(), e) { | ||
Some((Constant::F64(n), _)) => n.is_nan(), | ||
Some((Constant::F32(n), _)) => n.is_nan(), | ||
_ => false, | ||
} | ||
} |
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
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,15 @@ | ||
### What it does | ||
Checks for a known NaN float being cast to an integer | ||
|
||
### Why is this bad? | ||
NaNs are cast into zero, so one could simply use this and make the | ||
code more readable. The lint could also hint at a programmer error. | ||
|
||
### Example | ||
``` | ||
let _: (0.0_f32 / 0.0) as u64; | ||
``` | ||
Use instead: | ||
``` | ||
let _: = 0_u64; | ||
``` |
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,18 @@ | ||
#![warn(clippy::cast_nan_to_int)] | ||
#![allow(clippy::eq_op)] | ||
|
||
fn main() { | ||
let _ = (0.0_f32 / -0.0) as usize; | ||
let _ = (f64::INFINITY * -0.0) as usize; | ||
let _ = (0.0 * f32::INFINITY) as usize; | ||
|
||
let _ = (f64::INFINITY + f64::NEG_INFINITY) as usize; | ||
let _ = (f32::INFINITY - f32::INFINITY) as usize; | ||
let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize; | ||
|
||
// those won't be linted: | ||
let _ = (1.0_f32 / 0.0) as usize; | ||
let _ = (f32::INFINITY * f32::NEG_INFINITY) as usize; | ||
let _ = (f32::INFINITY - f32::NEG_INFINITY) as usize; | ||
let _ = (f64::INFINITY - 0.0) as usize; | ||
} |
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,51 @@ | ||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:5:13 | ||
| | ||
LL | let _ = (0.0_f32 / -0.0) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
= note: `-D clippy::cast-nan-to-int` implied by `-D warnings` | ||
|
||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:6:13 | ||
| | ||
LL | let _ = (f64::INFINITY * -0.0) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
|
||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:7:13 | ||
| | ||
LL | let _ = (0.0 * f32::INFINITY) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
|
||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:9:13 | ||
| | ||
LL | let _ = (f64::INFINITY + f64::NEG_INFINITY) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
|
||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:10:13 | ||
| | ||
LL | let _ = (f32::INFINITY - f32::INFINITY) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
|
||
error: casting a known NaN to usize | ||
--> $DIR/cast_nan_to_int.rs:11:13 | ||
| | ||
LL | let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this always evaluates to 0 | ||
|
||
error: aborting due to 6 previous errors | ||
|