@@ -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 ! {
@@ -74,29 +74,15 @@ impl ApproxConstant {
7474 }
7575 }
7676
77- fn check_lit ( & self , cx : & LateContext < ' _ > , lit : & LitKind , e : & Expr < ' _ > ) {
78- match * lit {
79- 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" ) ,
84- } ,
85- // 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}" ) ,
87- _ => ( ) ,
88- }
89- }
90-
91- fn check_known_consts ( & self , cx : & LateContext < ' _ > , e : & Expr < ' _ > , s : symbol:: Symbol , module : & str ) {
77+ fn check_known_consts ( & self , cx : & LateContext < ' _ > , span : Span , s : symbol:: Symbol , module : & str ) {
9278 let s = s. as_str ( ) ;
9379 if s. parse :: < f64 > ( ) . is_ok ( ) {
9480 for & ( constant, name, min_digits, msrv) in & KNOWN_CONSTS {
9581 if is_approx_const ( constant, s, min_digits) && msrv. is_none_or ( |msrv| self . msrv . meets ( msrv) ) {
9682 span_lint_and_help (
9783 cx,
9884 APPROX_CONSTANT ,
99- e . span ,
85+ span,
10086 format ! ( "approximate value of `{module}::consts::{name}` found" ) ,
10187 None ,
10288 "consider using the constant directly" ,
@@ -111,9 +97,17 @@ impl ApproxConstant {
11197impl_lint_pass ! ( ApproxConstant => [ APPROX_CONSTANT ] ) ;
11298
11399impl < ' 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) ;
100+ fn check_lit ( & mut self , cx : & LateContext < ' _ > , _hir_id : HirId , lit : & Lit , _negated : bool ) {
101+ match lit. node {
102+ LitKind :: Float ( s, LitFloatType :: Suffixed ( fty) ) => match fty {
103+ FloatTy :: F16 => self . check_known_consts ( cx, lit. span , s, "f16" ) ,
104+ FloatTy :: F32 => self . check_known_consts ( cx, lit. span , s, "f32" ) ,
105+ FloatTy :: F64 => self . check_known_consts ( cx, lit. span , s, "f64" ) ,
106+ FloatTy :: F128 => self . check_known_consts ( cx, lit. span , s, "f128" ) ,
107+ } ,
108+ // FIXME(f16_f128): add `f16` and `f128` when these types become stable.
109+ LitKind :: Float ( s, LitFloatType :: Unsuffixed ) => self . check_known_consts ( cx, lit. span , s, "f{32, 64}" ) ,
110+ _ => ( ) ,
117111 }
118112 }
119113
0 commit comments