1+ use  clippy_config:: Conf ; 
12use  clippy_utils:: diagnostics:: span_lint_and_then; 
23use  clippy_utils:: { ExprUseNode ,  expr_use_ctxt,  numeric_literal} ; 
34use  rustc_ast:: ast:: { self ,  LitFloatType ,  LitKind } ; 
45use  rustc_errors:: Applicability ; 
56use  rustc_hir as  hir; 
67use  rustc_lint:: { LateContext ,  LateLintPass } ; 
78use  rustc_middle:: ty:: { self ,  FloatTy } ; 
8- use  rustc_session:: declare_lint_pass ; 
9+ use  rustc_session:: impl_lint_pass ; 
910use  std:: fmt; 
1011
1112declare_clippy_lint !  { 
1213    /// ### What it does 
1314/// Checks for float literals with a precision greater 
1415/// than that supported by the underlying type. 
1516/// 
16- /// The lint is suppressed for literals with over 40  digits. 
17+ /// The lint is suppressed for literals with over `const_literal_precision_threshold`  digits. 
1718/// 
1819/// ### Why is this bad? 
1920/// Rust will truncate the literal silently. 
@@ -60,7 +61,21 @@ declare_clippy_lint! {
6061    "lossy whole number float literals" 
6162} 
6263
63- declare_lint_pass ! ( FloatLiteral  => [ EXCESSIVE_PRECISION ,  LOSSY_FLOAT_LITERAL ] ) ; 
64+ pub  struct  FloatLiteral  { 
65+     const_literal_precision_threshold :  usize , 
66+ } 
67+ 
68+ impl_lint_pass ! ( FloatLiteral  => [ 
69+     EXCESSIVE_PRECISION ,  LOSSY_FLOAT_LITERAL 
70+ ] ) ; 
71+ 
72+ impl  FloatLiteral  { 
73+     pub  fn  new ( conf :  & ' static  Conf )  -> Self  { 
74+         Self  { 
75+             const_literal_precision_threshold :  conf. const_literal_precision_threshold , 
76+         } 
77+     } 
78+ } 
6479
6580impl < ' tcx >  LateLintPass < ' tcx >  for  FloatLiteral  { 
6681    fn  check_expr ( & mut  self ,  cx :  & LateContext < ' tcx > ,  expr :  & ' tcx  hir:: Expr < ' _ > )  { 
@@ -129,7 +144,9 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
129144                    ) ; 
130145                } 
131146            }  else  if  digits > max as  usize  && count_digits ( & float_str)  < digits { 
132-                 if  digits >= 40  && matches ! ( expr_use_ctxt( cx,  expr) . use_node( cx) ,  ExprUseNode :: ConstStatic ( _) )  { 
147+                 if  digits >= self . const_literal_precision_threshold 
148+                     && matches ! ( expr_use_ctxt( cx,  expr) . use_node( cx) ,  ExprUseNode :: ConstStatic ( _) ) 
149+                 { 
133150                    // If a big enough number of digits is specified and it's a constant 
134151                    // we assume the user is definining a constant, and excessive precision is ok 
135152                    return ; 
@@ -140,7 +157,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
140157                    expr. span , 
141158                    "float has excessive precision" , 
142159                    |diag| { 
143-                         if  digits >= 40 
160+                         if  digits >= self . const_literal_precision_threshold 
144161                            && let  Some ( let_stmt)  = maybe_let_stmt ( cx,  expr) 
145162                        { 
146163                            diag. span_note ( let_stmt. span ,  "consider making it a `const` item" ) ; 
0 commit comments