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
@@ -30,38 +33,40 @@ declare_clippy_lint! {
3033 "the approximate of a known float constant (in `std::fXX::consts`)"
3134}
3235
36+ lazy_static ! {
3337// Tuples are of the form (constant, name, min_digits)
34- const KNOWN_CONSTS : & [ ( f64 , & str , usize ) ] = & [
35- ( f64:: E , "E" , 4 ) ,
36- ( f64:: FRAC_1_PI , " FRAC_1_PI" , 4 ) ,
37- ( f64:: FRAC_1_SQRT_2 , " FRAC_1_SQRT_2" , 5 ) ,
38- ( f64:: FRAC_2_PI , " FRAC_2_PI" , 5 ) ,
39- ( f64:: FRAC_2_SQRT_PI , " FRAC_2_SQRT_PI" , 5 ) ,
40- ( f64:: FRAC_PI_2 , " FRAC_PI_2" , 5 ) ,
41- ( f64:: FRAC_PI_3 , " FRAC_PI_3" , 5 ) ,
42- ( f64:: FRAC_PI_4 , " FRAC_PI_4" , 5 ) ,
43- ( f64:: FRAC_PI_6 , " FRAC_PI_6" , 5 ) ,
44- ( f64:: FRAC_PI_8 , " FRAC_PI_8" , 5 ) ,
45- ( f64:: LN_10 , " LN_10" , 5 ) ,
46- ( f64:: LN_2 , " LN_2" , 5 ) ,
47- ( f64:: LOG10_E , " LOG10_E" , 5 ) ,
48- ( f64:: LOG2_E , " LOG2_E" , 5 ) ,
49- ( f64:: PI , "PI" , 3 ) ,
50- ( 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 ) ,
5155] ;
56+ }
5257
5358declare_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
5459
5560impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for ApproxConstant {
5661 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr ) {
5762 if let ExprKind :: Lit ( lit) = & e. node {
58- check_lit ( cx, lit, e) ;
63+ check_lit ( cx, & lit. node , e) ;
5964 }
6065 }
6166}
6267
63- fn check_lit ( cx : & LateContext < ' _ , ' _ > , lit : & Lit , e : & Expr ) {
64- match lit. node {
68+ fn check_lit ( cx : & LateContext < ' _ , ' _ > , lit : & LitKind , e : & Expr ) {
69+ match * lit {
6570 LitKind :: Float ( s, FloatTy :: F32 ) => check_known_consts ( cx, e, s, "f32" ) ,
6671 LitKind :: Float ( s, FloatTy :: F64 ) => check_known_consts ( cx, e, s, "f64" ) ,
6772 LitKind :: FloatUnsuffixed ( s) => check_known_consts ( cx, e, s, "f{32, 64}" ) ,
@@ -72,7 +77,7 @@ fn check_lit(cx: &LateContext<'_, '_>, lit: &Lit, e: &Expr) {
7277fn check_known_consts ( cx : & LateContext < ' _ , ' _ > , e : & Expr , s : symbol:: Symbol , module : & str ) {
7378 let s = s. as_str ( ) ;
7479 if s. parse :: < f64 > ( ) . is_ok ( ) {
75- for & ( constant, name, min_digits) in KNOWN_CONSTS {
80+ for & ( constant, name, min_digits) in KNOWN_CONSTS . iter ( ) {
7681 if is_approx_const ( constant, & s, min_digits) {
7782 span_lint (
7883 cx,
0 commit comments