1
1
use crate :: utils:: span_lint;
2
+ use crate :: utils:: sym;
3
+ use lazy_static:: lazy_static;
2
4
use rustc:: hir:: * ;
3
5
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
4
6
use rustc:: { declare_lint_pass, declare_tool_lint} ;
5
7
use std:: f64:: consts as f64;
6
- use syntax:: ast:: { FloatTy , Lit , LitKind } ;
8
+ use syntax:: ast:: { FloatTy , LitKind } ;
7
9
use syntax:: symbol;
10
+ use syntax:: symbol:: Symbol ;
8
11
9
12
declare_clippy_lint ! {
10
13
/// **What it does:** Checks for floating point literals that approximate
@@ -30,38 +33,40 @@ 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
55
60
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for ApproxConstant {
56
61
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr ) {
57
62
if let ExprKind :: Lit ( lit) = & e. node {
58
- check_lit ( cx, lit, e) ;
63
+ check_lit ( cx, & lit. node , e) ;
59
64
}
60
65
}
61
66
}
62
67
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 {
65
70
LitKind :: Float ( s, FloatTy :: F32 ) => check_known_consts ( cx, e, s, "f32" ) ,
66
71
LitKind :: Float ( s, FloatTy :: F64 ) => check_known_consts ( cx, e, s, "f64" ) ,
67
72
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) {
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