11use crate :: utils:: span_lint;
2- use crate :: utils:: sym;
3- use lazy_static:: lazy_static;
42use rustc:: hir:: * ;
53use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
64use rustc:: { declare_lint_pass, declare_tool_lint} ;
75use std:: f64:: consts as f64;
86use syntax:: ast:: { FloatTy , LitKind } ;
97use syntax:: symbol;
10- use syntax:: symbol:: Symbol ;
118
129declare_clippy_lint ! {
1310 /// **What it does:** Checks for floating point literals that approximate
@@ -33,27 +30,25 @@ declare_clippy_lint! {
3330 "the approximate of a known float constant (in `std::fXX::consts`)"
3431}
3532
36- lazy_static ! {
3733// Tuples are of the form (constant, name, min_digits)
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 ) ,
34+ const KNOWN_CONSTS : [ ( f64 , & str , usize ) ; 16 ] = [
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 ) ,
5551] ;
56- }
5752
5853declare_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
5954
@@ -77,7 +72,7 @@ fn check_lit(cx: &LateContext<'_, '_>, lit: &LitKind, e: &Expr) {
7772fn check_known_consts ( cx : & LateContext < ' _ , ' _ > , e : & Expr , s : symbol:: Symbol , module : & str ) {
7873 let s = s. as_str ( ) ;
7974 if s. parse :: < f64 > ( ) . is_ok ( ) {
80- for & ( constant, name, min_digits) in KNOWN_CONSTS . iter ( ) {
75+ for & ( constant, name, min_digits) in & KNOWN_CONSTS {
8176 if is_approx_const ( constant, & s, min_digits) {
8277 span_lint (
8378 cx,
0 commit comments