11use crate :: utils:: span_lint;
2+ use crate :: utils:: sym;
3+ use lazy_static:: lazy_static;
24use rustc:: hir:: * ;
35use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
46use rustc:: { declare_lint_pass, declare_tool_lint} ;
57use std:: f64:: consts as f64;
6- use syntax:: ast:: { FloatTy , Lit , LitKind } ;
8+ use syntax:: ast:: { FloatTy , LitKind } ;
79use syntax:: symbol;
10+ use syntax:: symbol:: Symbol ;
811
912declare_clippy_lint ! {
1013 /// **What it does:** Checks for floating point literals that approximate
@@ -19,10 +22,7 @@ declare_clippy_lint! {
1922 /// actually more precise, please [file a Rust
2023 /// issue](https://github.com/rust-lang/rust/issues).
2124 ///
22- /// **Known problems:** If you happen to have a value that is within 1/8192 of a
23- /// known constant, but is not *and should not* be the same, this lint will
24- /// report your value anyway. We have not yet noticed any false positives in
25- /// code we tested clippy with (this includes servo), but YMMV.
25+ /// **Known problems:** None.
2626 ///
2727 /// **Example:**
2828 /// ```rust
@@ -33,38 +33,40 @@ declare_clippy_lint! {
3333 "the approximate of a known float constant (in `std::fXX::consts`)"
3434}
3535
36+ lazy_static ! {
3637// Tuples are of the form (constant, name, min_digits)
37- const KNOWN_CONSTS : & [ ( f64 , & str , usize ) ] = & [
38- ( f64:: E , "E" , 4 ) ,
39- ( f64:: FRAC_1_PI , " FRAC_1_PI" , 4 ) ,
40- ( f64:: FRAC_1_SQRT_2 , " FRAC_1_SQRT_2" , 5 ) ,
41- ( f64:: FRAC_2_PI , " FRAC_2_PI" , 5 ) ,
42- ( f64:: FRAC_2_SQRT_PI , " FRAC_2_SQRT_PI" , 5 ) ,
43- ( f64:: FRAC_PI_2 , " FRAC_PI_2" , 5 ) ,
44- ( f64:: FRAC_PI_3 , " FRAC_PI_3" , 5 ) ,
45- ( f64:: FRAC_PI_4 , " FRAC_PI_4" , 5 ) ,
46- ( f64:: FRAC_PI_6 , " FRAC_PI_6" , 5 ) ,
47- ( f64:: FRAC_PI_8 , " FRAC_PI_8" , 5 ) ,
48- ( f64:: LN_10 , " LN_10" , 5 ) ,
49- ( f64:: LN_2 , " LN_2" , 5 ) ,
50- ( f64:: LOG10_E , " LOG10_E" , 5 ) ,
51- ( f64:: LOG2_E , " LOG2_E" , 5 ) ,
52- ( f64:: PI , "PI" , 3 ) ,
53- ( f64:: SQRT_2 , " SQRT_2" , 5 ) ,
38+ static ref KNOWN_CONSTS : [ ( f64 , Symbol , usize ) ; 16 ] = [
39+ ( f64 :: E , * sym :: E , 4 ) ,
40+ ( f64 :: FRAC_1_PI , * sym :: FRAC_1_PI , 4 ) ,
41+ ( f64 :: FRAC_1_SQRT_2 , * sym :: FRAC_1_SQRT_2 , 5 ) ,
42+ ( f64 :: FRAC_2_PI , * sym :: FRAC_2_PI , 5 ) ,
43+ ( f64 :: FRAC_2_SQRT_PI , * sym :: FRAC_2_SQRT_PI , 5 ) ,
44+ ( f64 :: FRAC_PI_2 , * sym :: FRAC_PI_2 , 5 ) ,
45+ ( f64 :: FRAC_PI_3 , * sym :: FRAC_PI_3 , 5 ) ,
46+ ( f64 :: FRAC_PI_4 , * sym :: FRAC_PI_4 , 5 ) ,
47+ ( f64 :: FRAC_PI_6 , * sym :: FRAC_PI_6 , 5 ) ,
48+ ( f64 :: FRAC_PI_8 , * sym :: FRAC_PI_8 , 5 ) ,
49+ ( f64 :: LN_10 , * sym :: LN_10 , 5 ) ,
50+ ( f64 :: LN_2 , * sym :: LN_2 , 5 ) ,
51+ ( f64 :: LOG10_E , * sym :: LOG10_E , 5 ) ,
52+ ( f64 :: LOG2_E , * sym :: LOG2_E , 5 ) ,
53+ ( f64 :: PI , * sym :: PI , 3 ) ,
54+ ( f64 :: SQRT_2 , * sym :: SQRT_2 , 5 ) ,
5455] ;
56+ }
5557
5658declare_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
5759
5860impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for ApproxConstant {
5961 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr ) {
6062 if let ExprKind :: Lit ( lit) = & e. node {
61- check_lit ( cx, lit, e) ;
63+ check_lit ( cx, & lit. node , e) ;
6264 }
6365 }
6466}
6567
66- fn check_lit ( cx : & LateContext < ' _ , ' _ > , lit : & Lit , e : & Expr ) {
67- match lit. node {
68+ fn check_lit ( cx : & LateContext < ' _ , ' _ > , lit : & LitKind , e : & Expr ) {
69+ match * lit {
6870 LitKind :: Float ( s, FloatTy :: F32 ) => check_known_consts ( cx, e, s, "f32" ) ,
6971 LitKind :: Float ( s, FloatTy :: F64 ) => check_known_consts ( cx, e, s, "f64" ) ,
7072 LitKind :: FloatUnsuffixed ( s) => check_known_consts ( cx, e, s, "f{32, 64}" ) ,
@@ -75,7 +77,7 @@ fn check_lit(cx: &LateContext<'_, '_>, lit: &Lit, e: &Expr) {
7577fn check_known_consts ( cx : & LateContext < ' _ , ' _ > , e : & Expr , s : symbol:: Symbol , module : & str ) {
7678 let s = s. as_str ( ) ;
7779 if s. parse :: < f64 > ( ) . is_ok ( ) {
78- for & ( constant, name, min_digits) in KNOWN_CONSTS {
80+ for & ( constant, name, min_digits) in KNOWN_CONSTS . iter ( ) {
7981 if is_approx_const ( constant, & s, min_digits) {
8082 span_lint (
8183 cx,
0 commit comments