Skip to content

Commit

Permalink
Auto merge of rust-lang#12168 - y21:issue12159, r=blyxyas
Browse files Browse the repository at this point in the history
[`default_numeric_fallback`]: improve const context detection

Fixes rust-lang#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
  • Loading branch information
bors committed Jan 18, 2024
2 parents bb2d497 + efd8daf commit b08ffee
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
14 changes: 7 additions & 7 deletions clippy_lints/src/default_numeric_fallback.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/ui/default_numeric_fallback_i32.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
40 changes: 40 additions & 0 deletions tests/ui/default_numeric_fallback_i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

0 comments on commit b08ffee

Please sign in to comment.