1
Fork 0

Use assoc const NAN for zero_div_zero lint

This commit is contained in:
Linus Färnstrand 2020-04-07 23:41:00 +02:00
parent 645b62e436
commit 51bb1d28c5
4 changed files with 10 additions and 11 deletions

View file

@ -60,7 +60,7 @@ pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<Mult
/// 6 | let other_f64_nan = 0.0f64 / 0.0; /// 6 | let other_f64_nan = 0.0f64 / 0.0;
/// | ^^^^^^^^^^^^ /// | ^^^^^^^^^^^^
/// | /// |
/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN /// = help: Consider using `f64::NAN` if you would like a constant representing NaN
/// ``` /// ```
pub fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) { pub fn span_lint_and_help<'a, T: LintContext>(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) {
cx.struct_span_lint(lint, span, |ldb| { cx.struct_span_lint(lint, span, |ldb| {

View file

@ -8,8 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for `0.0 / 0.0`. /// **What it does:** Checks for `0.0 / 0.0`.
/// ///
/// **Why is this bad?** It's less readable than `std::f32::NAN` or /// **Why is this bad?** It's less readable than `f32::NAN` or `f64::NAN`.
/// `std::f64::NAN`.
/// ///
/// **Known problems:** None. /// **Known problems:** None.
/// ///
@ -19,7 +18,7 @@ declare_clippy_lint! {
/// ``` /// ```
pub ZERO_DIVIDED_BY_ZERO, pub ZERO_DIVIDED_BY_ZERO,
complexity, complexity,
"usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`" "usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`"
} }
declare_lint_pass!(ZeroDiv => [ZERO_DIVIDED_BY_ZERO]); declare_lint_pass!(ZeroDiv => [ZERO_DIVIDED_BY_ZERO]);
@ -38,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value; if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value;
if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value; if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value;
then { then {
// since we're about to suggest a use of std::f32::NaN or std::f64::NaN, // since we're about to suggest a use of f32::NAN or f64::NAN,
// match the precision of the literals that are given. // match the precision of the literals that are given.
let float_type = match (lhs_value, rhs_value) { let float_type = match (lhs_value, rhs_value) {
(Constant::F64(_), _) (Constant::F64(_), _)
@ -51,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ZeroDiv {
expr.span, expr.span,
"constant division of `0.0` with `0.0` will always result in NaN", "constant division of `0.0` with `0.0` will always result in NaN",
&format!( &format!(
"Consider using `std::{}::NAN` if you would like a constant representing NaN", "Consider using `{}::NAN` if you would like a constant representing NaN",
float_type, float_type,
), ),
); );

View file

@ -2526,7 +2526,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
Lint { Lint {
name: "zero_divided_by_zero", name: "zero_divided_by_zero",
group: "complexity", group: "complexity",
desc: "usage of `0.0 / 0.0` to obtain NaN instead of `std::f32::NAN` or `std::f64::NAN`", desc: "usage of `0.0 / 0.0` to obtain NaN instead of `f32::NAN` or `f64::NAN`",
deprecation: None, deprecation: None,
module: "zero_div_zero", module: "zero_div_zero",
}, },

View file

@ -13,7 +13,7 @@ LL | let nan = 0.0 / 0.0;
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: `-D clippy::zero-divided-by-zero` implied by `-D warnings` = note: `-D clippy::zero-divided-by-zero` implied by `-D warnings`
= help: Consider using `std::f64::NAN` if you would like a constant representing NaN = help: Consider using `f64::NAN` if you would like a constant representing NaN
error: equal expressions as operands to `/` error: equal expressions as operands to `/`
--> $DIR/zero_div_zero.rs:5:19 --> $DIR/zero_div_zero.rs:5:19
@ -27,7 +27,7 @@ error: constant division of `0.0` with `0.0` will always result in NaN
LL | let f64_nan = 0.0 / 0.0f64; LL | let f64_nan = 0.0 / 0.0f64;
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |
= help: Consider using `std::f64::NAN` if you would like a constant representing NaN = help: Consider using `f64::NAN` if you would like a constant representing NaN
error: equal expressions as operands to `/` error: equal expressions as operands to `/`
--> $DIR/zero_div_zero.rs:6:25 --> $DIR/zero_div_zero.rs:6:25
@ -41,7 +41,7 @@ error: constant division of `0.0` with `0.0` will always result in NaN
LL | let other_f64_nan = 0.0f64 / 0.0; LL | let other_f64_nan = 0.0f64 / 0.0;
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |
= help: Consider using `std::f64::NAN` if you would like a constant representing NaN = help: Consider using `f64::NAN` if you would like a constant representing NaN
error: equal expressions as operands to `/` error: equal expressions as operands to `/`
--> $DIR/zero_div_zero.rs:7:28 --> $DIR/zero_div_zero.rs:7:28
@ -55,7 +55,7 @@ error: constant division of `0.0` with `0.0` will always result in NaN
LL | let one_more_f64_nan = 0.0f64 / 0.0f64; LL | let one_more_f64_nan = 0.0f64 / 0.0f64;
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
| |
= help: Consider using `std::f64::NAN` if you would like a constant representing NaN = help: Consider using `f64::NAN` if you would like a constant representing NaN
error: aborting due to 8 previous errors error: aborting due to 8 previous errors