File tree Expand file tree Collapse file tree 3 files changed +42
-10
lines changed Expand file tree Collapse file tree 3 files changed +42
-10
lines changed Original file line number Diff line number Diff line change 11use clippy_utils:: diagnostics:: span_lint_and_then;
2- use clippy_utils:: numeric_literal;
2+ use clippy_utils:: { ExprUseNode , expr_use_ctxt , numeric_literal} ;
33use rustc_ast:: ast:: { LitFloatType , LitKind } ;
44use rustc_errors:: Applicability ;
55use rustc_hir as hir;
@@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
129129 ) ;
130130 }
131131 } else if digits > max as usize && count_digits ( & float_str) < digits {
132- if digits >= 40 && is_const_item ( cx, expr) {
132+ if digits >= 40 && matches ! ( expr_use_ctxt ( cx, expr) . use_node ( cx ) , ExprUseNode :: ConstStatic ( _ ) ) {
133133 // If a big enough number of digits is specified and it's a constant
134134 // we assume the user is definining a constant, and excessive precision is ok
135135 return ;
@@ -209,14 +209,6 @@ impl FloatFormat {
209209 }
210210}
211211
212- fn is_const_item ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
213- let parent = cx. tcx . parent_hir_node ( expr. hir_id ) ;
214- if let hir:: Node :: Item ( itm) = parent {
215- return matches ! ( itm. kind, hir:: ItemKind :: Const ( ..) ) ;
216- }
217- false
218- }
219-
220212fn maybe_let_stmt < ' a > ( cx : & LateContext < ' a > , expr : & hir:: Expr < ' _ > ) -> Option < & ' a hir:: LetStmt < ' a > > {
221213 let parent = cx. tcx . parent_hir_node ( expr. hir_id ) ;
222214 match parent {
Original file line number Diff line number Diff line change @@ -109,4 +109,24 @@ fn main() {
109109 //~^ excessive_precision
110110 const _: f32 = 1.01234567890123456789012345678901234567890;
111111 const _: f64 = 1.01234567890123456789012345678901234567890;
112+
113+ static STATIC1: f32 = 1.01234567890123456789012345678901234567890;
114+ static STATIC2: f64 = 1.01234567890123456789012345678901234567890;
115+
116+ static mut STATIC_MUT1: f32 = 1.01234567890123456789012345678901234567890;
117+ static mut STATIC_MUT2: f64 = 1.01234567890123456789012345678901234567890;
118+ }
119+
120+ trait ExcessivelyPreciseTrait {
121+ // Overly specified constants
122+ const GOOD1: f32 = 1.01234567890123456789012345678901234567890;
123+ const GOOD2: f64 = 1.01234567890123456789012345678901234567890;
124+ }
125+
126+ struct ExcessivelyPreciseStruct;
127+
128+ impl ExcessivelyPreciseStruct {
129+ // Overly specified constants
130+ const GOOD1: f32 = 1.01234567890123456789012345678901234567890;
131+ const GOOD2: f64 = 1.01234567890123456789012345678901234567890;
112132}
Original file line number Diff line number Diff line change @@ -109,4 +109,24 @@ fn main() {
109109 //~^ excessive_precision
110110 const _: f32 = 1.01234567890123456789012345678901234567890 ;
111111 const _: f64 = 1.01234567890123456789012345678901234567890 ;
112+
113+ static STATIC1 : f32 = 1.01234567890123456789012345678901234567890 ;
114+ static STATIC2 : f64 = 1.01234567890123456789012345678901234567890 ;
115+
116+ static mut STATIC_MUT1 : f32 = 1.01234567890123456789012345678901234567890 ;
117+ static mut STATIC_MUT2 : f64 = 1.01234567890123456789012345678901234567890 ;
118+ }
119+
120+ trait ExcessivelyPreciseTrait {
121+ // Overly specified constants
122+ const GOOD1 : f32 = 1.01234567890123456789012345678901234567890 ;
123+ const GOOD2 : f64 = 1.01234567890123456789012345678901234567890 ;
124+ }
125+
126+ struct ExcessivelyPreciseStruct ;
127+
128+ impl ExcessivelyPreciseStruct {
129+ // Overly specified constants
130+ const GOOD1 : f32 = 1.01234567890123456789012345678901234567890 ;
131+ const GOOD2 : f64 = 1.01234567890123456789012345678901234567890 ;
112132}
You can’t perform that action at this time.
0 commit comments