]> git.lizzy.rs Git - rust.git/blob - src/approx_const.rs
Split `new_without_default` and `new_without_default_derive`.
[rust.git] / src / approx_const.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use std::f64::consts as f64;
4 use syntax::ast::{Lit, LitKind, FloatTy};
5 use utils::span_lint;
6
7 /// **What it does:** This lint checks for floating point literals that approximate constants which are defined in [`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants) or [`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants), respectively, suggesting to use the predefined constant.
8 ///
9 /// **Why is this bad?** Usually, the definition in the standard library is more precise than what people come up with. If you find that your definition is actually more precise, please [file a Rust issue](https://github.com/rust-lang/rust/issues).
10 ///
11 /// **Known problems:** If you happen to have a value that is within 1/8192 of a known constant, but is not *and should not* be the same, this lint will report your value anyway. We have not yet noticed any false positives in code we tested clippy with (this includes servo), but YMMV.
12 ///
13 /// **Example:** `let x = 3.14;`
14 declare_lint! {
15     pub APPROX_CONSTANT,
16     Warn,
17     "the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) \
18      is found; suggests to use the constant"
19 }
20
21 // Tuples are of the form (constant, name, min_digits)
22 const KNOWN_CONSTS: &'static [(f64, &'static str, usize)] = &[(f64::E, "E", 4),
23                                                               (f64::FRAC_1_PI, "FRAC_1_PI", 4),
24                                                               (f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5),
25                                                               (f64::FRAC_2_PI, "FRAC_2_PI", 5),
26                                                               (f64::FRAC_2_SQRT_PI, "FRAC_2_SQRT_PI", 5),
27                                                               (f64::FRAC_PI_2, "FRAC_PI_2", 5),
28                                                               (f64::FRAC_PI_3, "FRAC_PI_3", 5),
29                                                               (f64::FRAC_PI_4, "FRAC_PI_4", 5),
30                                                               (f64::FRAC_PI_6, "FRAC_PI_6", 5),
31                                                               (f64::FRAC_PI_8, "FRAC_PI_8", 5),
32                                                               (f64::LN_10, "LN_10", 5),
33                                                               (f64::LN_2, "LN_2", 5),
34                                                               (f64::LOG10_E, "LOG10_E", 5),
35                                                               (f64::LOG2_E, "LOG2_E", 5),
36                                                               (f64::PI, "PI", 3),
37                                                               (f64::SQRT_2, "SQRT_2", 5)];
38
39 #[derive(Copy,Clone)]
40 pub struct ApproxConstant;
41
42 impl LintPass for ApproxConstant {
43     fn get_lints(&self) -> LintArray {
44         lint_array!(APPROX_CONSTANT)
45     }
46 }
47
48 impl LateLintPass for ApproxConstant {
49     fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
50         if let ExprLit(ref lit) = e.node {
51             check_lit(cx, lit, e);
52         }
53     }
54 }
55
56 fn check_lit(cx: &LateContext, lit: &Lit, e: &Expr) {
57     match lit.node {
58         LitKind::Float(ref s, FloatTy::F32) => check_known_consts(cx, e, s, "f32"),
59         LitKind::Float(ref s, FloatTy::F64) => check_known_consts(cx, e, s, "f64"),
60         LitKind::FloatUnsuffixed(ref s) => check_known_consts(cx, e, s, "f{32, 64}"),
61         _ => (),
62     }
63 }
64
65 fn check_known_consts(cx: &LateContext, e: &Expr, s: &str, module: &str) {
66     if let Ok(_) = s.parse::<f64>() {
67         for &(constant, name, min_digits) in KNOWN_CONSTS {
68             if is_approx_const(constant, s, min_digits) {
69                 span_lint(cx,
70                           APPROX_CONSTANT,
71                           e.span,
72                           &format!("approximate value of `{}::{}` found. Consider using it directly", module, &name));
73                 return;
74             }
75         }
76     }
77 }
78
79 /// Returns false if the number of significant figures in `value` are
80 /// less than `min_digits`; otherwise, returns true if `value` is equal
81 /// to `constant`, rounded to the number of digits present in `value`.
82 fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {
83     if value.len() <= min_digits {
84         false
85     } else {
86         let round_const = format!("{:.*}", value.len() - 2, constant);
87
88         let mut trunc_const = constant.to_string();
89         if trunc_const.len() > value.len() {
90             trunc_const.truncate(value.len());
91         }
92
93         (value == round_const) || (value == trunc_const)
94     }
95 }