1
1
use crate :: utils:: span_lint;
2
+ use crate :: utils:: sym;
2
3
use rustc:: hir:: * ;
3
4
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
4
5
use rustc:: { declare_lint_pass, declare_tool_lint} ;
5
6
use std:: f64:: consts as f64;
6
- use syntax:: ast:: { FloatTy , Lit , LitKind } ;
7
+ use syntax:: ast:: { FloatTy , LitKind } ;
7
8
use syntax:: symbol;
9
+ use syntax:: symbol:: Symbol ;
10
+ use lazy_static:: lazy_static;
8
11
9
12
declare_clippy_lint ! {
10
13
/// **What it does:** Checks for floating point literals that approximate
@@ -30,25 +33,27 @@ declare_clippy_lint! {
30
33
"the approximate of a known float constant (in `std::fXX::consts`)"
31
34
}
32
35
36
+ lazy_static ! {
33
37
// 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 ) ,
51
55
] ;
56
+ }
52
57
53
58
declare_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
54
59
@@ -72,7 +77,7 @@ fn check_lit(cx: &LateContext<'_, '_>, lit: &LitKind, e: &Expr) {
72
77
fn check_known_consts ( cx : & LateContext < ' _ , ' _ > , e : & Expr , s : symbol:: Symbol , module : & str ) {
73
78
let s = s. as_str ( ) ;
74
79
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 ( ) {
76
81
if is_approx_const ( constant, & s, min_digits) {
77
82
span_lint (
78
83
cx,
0 commit comments