Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cast-nan-to-int lint #9614

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3772,6 +3772,7 @@ Released 2018-09-13
[`cast_enum_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_constructor
[`cast_enum_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_truncation
[`cast_lossless`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
[`cast_nan_to_int`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_nan_to_int
[`cast_possible_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
[`cast_possible_wrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
[`cast_precision_loss`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_precision_loss
Expand Down
26 changes: 26 additions & 0 deletions clippy_lints/src/casts/cast_nan_to_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::CAST_NAN_TO_INT;

use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint;
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(
cx,
CAST_NAN_TO_INT,
expr.span,
&format!("casting a known NaN to {to_ty}"),
);
}
}

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,
}
}
28 changes: 27 additions & 1 deletion clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod borrow_as_ptr;
mod cast_abs_to_unsigned;
mod cast_enum_constructor;
mod cast_lossless;
mod cast_nan_to_int;
mod cast_possible_truncation;
mod cast_possible_wrap;
mod cast_precision_loss;
Expand Down Expand Up @@ -569,6 +570,7 @@ declare_clippy_lint! {
pedantic,
"borrowing just to cast to a raw pointer"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for a raw slice being cast to a slice pointer
Expand Down Expand Up @@ -596,6 +598,28 @@ declare_clippy_lint! {
"casting a slice created from a pointer and length to a slice pointer"
}

declare_clippy_lint! {
/// ### 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
/// ```rust,ignore
/// let _: (0.0_f32 / 0.0) as u64;
/// ```
/// Use instead:
/// ```rust,ignore
/// let _: = 0_u64;
/// ```
#[clippy::version = "1.64.0"]
pub CAST_NAN_TO_INT,
suspicious,
"casting a known floating-point NaN into an integer"
}

pub struct Casts {
msrv: Option<RustcVersion>,
}
Expand Down Expand Up @@ -627,7 +651,8 @@ impl_lint_pass!(Casts => [
CAST_ABS_TO_UNSIGNED,
AS_UNDERSCORE,
BORROW_AS_PTR,
CAST_SLICE_FROM_RAW_PARTS
CAST_SLICE_FROM_RAW_PARTS,
CAST_NAN_TO_INT,
]);

impl<'tcx> LateLintPass<'tcx> for Casts {
Expand Down Expand Up @@ -664,6 +689,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
cast_precision_loss::check(cx, expr, cast_from, cast_to);
cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to);
cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
cast_nan_to_int::check(cx, expr, cast_expr, cast_from, cast_to);
}
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
cast_enum_constructor::check(cx, expr, cast_expr, cast_from);
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
LintId::of(casts::CAST_ENUM_TRUNCATION),
LintId::of(casts::CAST_NAN_TO_INT),
LintId::of(casts::CAST_REF_TO_MUT),
LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES),
LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS),
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ store.register_lints(&[
casts::CAST_ENUM_CONSTRUCTOR,
casts::CAST_ENUM_TRUNCATION,
casts::CAST_LOSSLESS,
casts::CAST_NAN_TO_INT,
casts::CAST_POSSIBLE_TRUNCATION,
casts::CAST_POSSIBLE_WRAP,
casts::CAST_PRECISION_LOSS,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_suspicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
LintId::of(casts::CAST_ENUM_TRUNCATION),
LintId::of(casts::CAST_NAN_TO_INT),
LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS),
LintId::of(crate_in_macro_def::CRATE_IN_MACRO_DEF),
LintId::of(drop_forget_ref::DROP_NON_DROP),
Expand Down
1 change: 1 addition & 0 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ docs! {
"cast_enum_constructor",
"cast_enum_truncation",
"cast_lossless",
"cast_nan_to_int",
"cast_possible_truncation",
"cast_possible_wrap",
"cast_precision_loss",
Expand Down
15 changes: 15 additions & 0 deletions src/docs/cast_nan_to_int.txt
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;
```
18 changes: 18 additions & 0 deletions tests/ui/cast_nan_to_int.rs
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;
}
40 changes: 40 additions & 0 deletions tests/ui/cast_nan_to_int.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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: `-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;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting a known NaN to usize
--> $DIR/cast_nan_to_int.rs:7:13
|
LL | let _ = (0.0 * f32::INFINITY) as usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting a known NaN to usize
--> $DIR/cast_nan_to_int.rs:9:13
|
LL | let _ = (f64::INFINITY + f64::NEG_INFINITY) as usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting a known NaN to usize
--> $DIR/cast_nan_to_int.rs:10:13
|
LL | let _ = (f32::INFINITY - f32::INFINITY) as usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting a known NaN to usize
--> $DIR/cast_nan_to_int.rs:11:13
|
LL | let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors