Skip to content

Commit 7ddd09d

Browse files
committed
Added const associated items matching and static
1 parent a52eeca commit 7ddd09d

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::numeric_literal;
2+
use clippy_utils::{ExprUseNode, expr_use_ctxt, numeric_literal};
33
use rustc_ast::ast::{LitFloatType, LitKind};
44
use rustc_errors::Applicability;
55
use 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-
220212
fn 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 {

tests/ui/excessive_precision.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/ui/excessive_precision.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)