@@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_help;
33use clippy_utils:: msrvs:: { self , Msrv } ;
44use rustc_ast:: ast:: { FloatTy , LitFloatType , LitKind } ;
55use rustc_attr_parsing:: RustcVersion ;
6- use rustc_hir:: { Expr , ExprKind } ;
6+ use rustc_hir:: { HirId , Lit } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
88use rustc_session:: impl_lint_pass;
9- use rustc_span:: symbol;
9+ use rustc_span:: { Span , symbol} ;
1010use std:: f64:: consts as f64;
1111
1212declare_clippy_lint ! {
@@ -73,30 +73,36 @@ impl ApproxConstant {
7373 msrv : conf. msrv . clone ( ) ,
7474 }
7575 }
76+ }
7677
77- fn check_lit ( & self , cx : & LateContext < ' _ > , lit : & LitKind , e : & Expr < ' _ > ) {
78- match * lit {
78+ impl < ' tcx > LateLintPass < ' tcx > for ApproxConstant {
79+ fn check_lit ( & mut self , cx : & LateContext < ' _ > , _hir_id : HirId , lit : & Lit , _negated : bool ) {
80+ match lit. node {
7981 LitKind :: Float ( s, LitFloatType :: Suffixed ( fty) ) => match fty {
80- FloatTy :: F16 => self . check_known_consts ( cx, e , s, "f16" ) ,
81- FloatTy :: F32 => self . check_known_consts ( cx, e , s, "f32" ) ,
82- FloatTy :: F64 => self . check_known_consts ( cx, e , s, "f64" ) ,
83- FloatTy :: F128 => self . check_known_consts ( cx, e , s, "f128" ) ,
82+ FloatTy :: F16 => self . check_known_consts ( cx, lit . span , s, "f16" ) ,
83+ FloatTy :: F32 => self . check_known_consts ( cx, lit . span , s, "f32" ) ,
84+ FloatTy :: F64 => self . check_known_consts ( cx, lit . span , s, "f64" ) ,
85+ FloatTy :: F128 => self . check_known_consts ( cx, lit . span , s, "f128" ) ,
8486 } ,
8587 // FIXME(f16_f128): add `f16` and `f128` when these types become stable.
86- LitKind :: Float ( s, LitFloatType :: Unsuffixed ) => self . check_known_consts ( cx, e , s, "f{32, 64}" ) ,
88+ LitKind :: Float ( s, LitFloatType :: Unsuffixed ) => self . check_known_consts ( cx, lit . span , s, "f{32, 64}" ) ,
8789 _ => ( ) ,
8890 }
8991 }
9092
91- fn check_known_consts ( & self , cx : & LateContext < ' _ > , e : & Expr < ' _ > , s : symbol:: Symbol , module : & str ) {
93+ extract_msrv_attr ! ( LateContext ) ;
94+ }
95+
96+ impl ApproxConstant {
97+ fn check_known_consts ( & self , cx : & LateContext < ' _ > , span : Span , s : symbol:: Symbol , module : & str ) {
9298 let s = s. as_str ( ) ;
9399 if s. parse :: < f64 > ( ) . is_ok ( ) {
94100 for & ( constant, name, min_digits, msrv) in & KNOWN_CONSTS {
95101 if is_approx_const ( constant, s, min_digits) && msrv. is_none_or ( |msrv| self . msrv . meets ( msrv) ) {
96102 span_lint_and_help (
97103 cx,
98104 APPROX_CONSTANT ,
99- e . span ,
105+ span,
100106 format ! ( "approximate value of `{module}::consts::{name}` found" ) ,
101107 None ,
102108 "consider using the constant directly" ,
@@ -110,16 +116,6 @@ impl ApproxConstant {
110116
111117impl_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
112118
113- impl < ' tcx > LateLintPass < ' tcx > for ApproxConstant {
114- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' _ > ) {
115- if let ExprKind :: Lit ( lit) = & e. kind {
116- self . check_lit ( cx, & lit. node , e) ;
117- }
118- }
119-
120- extract_msrv_attr ! ( LateContext ) ;
121- }
122-
123119/// Returns `false` if the number of significant figures in `value` are
124120/// less than `min_digits`; otherwise, returns true if `value` is equal
125121/// to `constant`, rounded to the number of digits present in `value`.
0 commit comments