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
bb2d497364/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
This commit is contained in:
bors 2024-01-18 16:55:31 +00:00
commit b08ffeee5c
3 changed files with 87 additions and 7 deletions

View File

@ -1,10 +1,10 @@
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::numeric_literal;
use clippy_utils::source::snippet_opt;
use clippy_utils::{get_parent_node, numeric_literal};
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_expr, walk_stmt, Visitor};
use rustc_hir::{Block, Body, Expr, ExprKind, FnRetTy, HirId, ItemKind, Lit, Node, Stmt, StmtKind};
use rustc_hir::{Block, Body, ConstContext, Expr, ExprKind, FnRetTy, HirId, Lit, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, FloatTy, IntTy, PolyFnSig, Ty};
@ -50,11 +50,11 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
let is_parent_const = if let Some(Node::Item(item)) = get_parent_node(cx.tcx, body.id().hir_id) {
matches!(item.kind, ItemKind::Const(..))
} else {
false
};
let hir = cx.tcx.hir();
let is_parent_const = matches!(
hir.body_const_context(hir.body_owner_def_id(body.id())),
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))
);
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
visitor.visit_body(body);
}

View File

@ -216,4 +216,44 @@ mod type_already_inferred {
}
}
mod issue12159 {
#![allow(non_upper_case_globals, clippy::exhaustive_structs)]
pub struct Foo;
static F: i32 = 1;
impl Foo {
const LIFE_u8: u8 = 42;
const LIFE_i8: i8 = 42;
const LIFE_u16: u16 = 42;
const LIFE_i16: i16 = 42;
const LIFE_u32: u32 = 42;
const LIFE_i32: i32 = 42;
const LIFE_u64: u64 = 42;
const LIFE_i64: i64 = 42;
const LIFE_u128: u128 = 42;
const LIFE_i128: i128 = 42;
const LIFE_usize: usize = 42;
const LIFE_isize: isize = 42;
const LIFE_f32: f32 = 42.;
const LIFE_f64: f64 = 42.;
const fn consts() {
const LIFE_u8: u8 = 42;
const LIFE_i8: i8 = 42;
const LIFE_u16: u16 = 42;
const LIFE_i16: i16 = 42;
const LIFE_u32: u32 = 42;
const LIFE_i32: i32 = 42;
const LIFE_u64: u64 = 42;
const LIFE_i64: i64 = 42;
const LIFE_u128: u128 = 42;
const LIFE_i128: i128 = 42;
const LIFE_usize: usize = 42;
const LIFE_isize: isize = 42;
const LIFE_f32: f32 = 42.;
const LIFE_f64: f64 = 42.;
}
}
}
fn main() {}

View File

@ -216,4 +216,44 @@ mod type_already_inferred {
}
}
mod issue12159 {
#![allow(non_upper_case_globals, clippy::exhaustive_structs)]
pub struct Foo;
static F: i32 = 1;
impl Foo {
const LIFE_u8: u8 = 42;
const LIFE_i8: i8 = 42;
const LIFE_u16: u16 = 42;
const LIFE_i16: i16 = 42;
const LIFE_u32: u32 = 42;
const LIFE_i32: i32 = 42;
const LIFE_u64: u64 = 42;
const LIFE_i64: i64 = 42;
const LIFE_u128: u128 = 42;
const LIFE_i128: i128 = 42;
const LIFE_usize: usize = 42;
const LIFE_isize: isize = 42;
const LIFE_f32: f32 = 42.;
const LIFE_f64: f64 = 42.;
const fn consts() {
const LIFE_u8: u8 = 42;
const LIFE_i8: i8 = 42;
const LIFE_u16: u16 = 42;
const LIFE_i16: i16 = 42;
const LIFE_u32: u32 = 42;
const LIFE_i32: i32 = 42;
const LIFE_u64: u64 = 42;
const LIFE_i64: i64 = 42;
const LIFE_u128: u128 = 42;
const LIFE_i128: i128 = 42;
const LIFE_usize: usize = 42;
const LIFE_isize: isize = 42;
const LIFE_f32: f32 = 42.;
const LIFE_f64: f64 = 42.;
}
}
}
fn main() {}