Skip to content

Commit b08ffee

Browse files
committed
Auto merge of #12168 - y21:issue12159, r=blyxyas
[`default_numeric_fallback`]: improve const context detection Fixes #12159 The lint didn't actually recognize any of the associated consts (in the linked issue), because in those cases the parent is an `ImplItem` and not an `Item`, but it only actually emitted a lint for i32 and f64 because the other cases failed the very last check here https://github.com/rust-lang/rust-clippy/blob/bb2d4973648b1af18d7ba6a3028ed7c92fde07fb/clippy_lints/src/default_numeric_fallback.rs#L91-L96 A better check for detecting constness would be using `body_const_context`, which is what this PR does. changelog: [`default_numeric_fallback`]: recognize associated consts
2 parents bb2d497 + efd8daf commit b08ffee

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2+
use clippy_utils::numeric_literal;
23
use clippy_utils::source::snippet_opt;
3-
use clippy_utils::{get_parent_node, numeric_literal};
44
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
55
use rustc_errors::Applicability;
66
use rustc_hir::intravisit::{walk_expr, walk_stmt, Visitor};
7-
use rustc_hir::{Block, Body, Expr, ExprKind, FnRetTy, HirId, ItemKind, Lit, Node, Stmt, StmtKind};
7+
use rustc_hir::{Block, Body, ConstContext, Expr, ExprKind, FnRetTy, HirId, Lit, Stmt, StmtKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_middle::ty::{self, FloatTy, IntTy, PolyFnSig, Ty};
@@ -50,11 +50,11 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
5050

5151
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
5252
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
53-
let is_parent_const = if let Some(Node::Item(item)) = get_parent_node(cx.tcx, body.id().hir_id) {
54-
matches!(item.kind, ItemKind::Const(..))
55-
} else {
56-
false
57-
};
53+
let hir = cx.tcx.hir();
54+
let is_parent_const = matches!(
55+
hir.body_const_context(hir.body_owner_def_id(body.id())),
56+
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))
57+
);
5858
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
5959
visitor.visit_body(body);
6060
}

tests/ui/default_numeric_fallback_i32.fixed

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,44 @@ mod type_already_inferred {
216216
}
217217
}
218218

219+
mod issue12159 {
220+
#![allow(non_upper_case_globals, clippy::exhaustive_structs)]
221+
pub struct Foo;
222+
223+
static F: i32 = 1;
224+
impl Foo {
225+
const LIFE_u8: u8 = 42;
226+
const LIFE_i8: i8 = 42;
227+
const LIFE_u16: u16 = 42;
228+
const LIFE_i16: i16 = 42;
229+
const LIFE_u32: u32 = 42;
230+
const LIFE_i32: i32 = 42;
231+
const LIFE_u64: u64 = 42;
232+
const LIFE_i64: i64 = 42;
233+
const LIFE_u128: u128 = 42;
234+
const LIFE_i128: i128 = 42;
235+
const LIFE_usize: usize = 42;
236+
const LIFE_isize: isize = 42;
237+
const LIFE_f32: f32 = 42.;
238+
const LIFE_f64: f64 = 42.;
239+
240+
const fn consts() {
241+
const LIFE_u8: u8 = 42;
242+
const LIFE_i8: i8 = 42;
243+
const LIFE_u16: u16 = 42;
244+
const LIFE_i16: i16 = 42;
245+
const LIFE_u32: u32 = 42;
246+
const LIFE_i32: i32 = 42;
247+
const LIFE_u64: u64 = 42;
248+
const LIFE_i64: i64 = 42;
249+
const LIFE_u128: u128 = 42;
250+
const LIFE_i128: i128 = 42;
251+
const LIFE_usize: usize = 42;
252+
const LIFE_isize: isize = 42;
253+
const LIFE_f32: f32 = 42.;
254+
const LIFE_f64: f64 = 42.;
255+
}
256+
}
257+
}
258+
219259
fn main() {}

tests/ui/default_numeric_fallback_i32.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,44 @@ mod type_already_inferred {
216216
}
217217
}
218218

219+
mod issue12159 {
220+
#![allow(non_upper_case_globals, clippy::exhaustive_structs)]
221+
pub struct Foo;
222+
223+
static F: i32 = 1;
224+
impl Foo {
225+
const LIFE_u8: u8 = 42;
226+
const LIFE_i8: i8 = 42;
227+
const LIFE_u16: u16 = 42;
228+
const LIFE_i16: i16 = 42;
229+
const LIFE_u32: u32 = 42;
230+
const LIFE_i32: i32 = 42;
231+
const LIFE_u64: u64 = 42;
232+
const LIFE_i64: i64 = 42;
233+
const LIFE_u128: u128 = 42;
234+
const LIFE_i128: i128 = 42;
235+
const LIFE_usize: usize = 42;
236+
const LIFE_isize: isize = 42;
237+
const LIFE_f32: f32 = 42.;
238+
const LIFE_f64: f64 = 42.;
239+
240+
const fn consts() {
241+
const LIFE_u8: u8 = 42;
242+
const LIFE_i8: i8 = 42;
243+
const LIFE_u16: u16 = 42;
244+
const LIFE_i16: i16 = 42;
245+
const LIFE_u32: u32 = 42;
246+
const LIFE_i32: i32 = 42;
247+
const LIFE_u64: u64 = 42;
248+
const LIFE_i64: i64 = 42;
249+
const LIFE_u128: u128 = 42;
250+
const LIFE_i128: i128 = 42;
251+
const LIFE_usize: usize = 42;
252+
const LIFE_isize: isize = 42;
253+
const LIFE_f32: f32 = 42.;
254+
const LIFE_f64: f64 = 42.;
255+
}
256+
}
257+
}
258+
219259
fn main() {}

0 commit comments

Comments
 (0)