1 use crate::consts::{constant_simple, Constant};
3 use rustc::{declare_lint, lint_array};
4 use if_chain::if_chain;
6 use crate::utils::span_help_and_lint;
8 /// **What it does:** Checks for `0.0 / 0.0`.
10 /// **Why is this bad?** It's less readable than `std::f32::NAN` or
13 /// **Known problems:** None.
19 declare_clippy_lint! {
20 pub ZERO_DIVIDED_BY_ZERO,
22 "usage of `0.0 / 0.0` to obtain NaN instead of std::f32::NaN or std::f64::NaN"
27 impl LintPass for Pass {
28 fn get_lints(&self) -> LintArray {
29 lint_array!(ZERO_DIVIDED_BY_ZERO)
33 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
34 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
35 // check for instances of 0.0/0.0
37 if let ExprKind::Binary(ref op, ref left, ref right) = expr.node;
38 if let BinOpKind::Div = op.node;
39 // TODO - constant_simple does not fold many operations involving floats.
40 // That's probably fine for this lint - it's pretty unlikely that someone would
41 // do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.
42 if let Some(lhs_value) = constant_simple(cx, cx.tables, left);
43 if let Some(rhs_value) = constant_simple(cx, cx.tables, right);
44 if Constant::F32(0.0) == lhs_value || Constant::F64(0.0) == lhs_value;
45 if Constant::F32(0.0) == rhs_value || Constant::F64(0.0) == rhs_value;
47 // since we're about to suggest a use of std::f32::NaN or std::f64::NaN,
48 // match the precision of the literals that are given.
49 let float_type = match (lhs_value, rhs_value) {
51 | (_, Constant::F64(_)) => "f64",
58 "constant division of 0.0 with 0.0 will always result in NaN",
60 "Consider using `std::{}::NAN` if you would like a constant representing NaN",