Skip to content

Commit 518591f

Browse files
committed
add utils is_inside_always_const_context
1 parent 3b4c263 commit 518591f

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
5151
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
5252
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
5353
let hir = cx.tcx.hir();
54+
// FIXME: maybe use `clippy_utils::is_inside_always_const_context` instead.
5455
let is_parent_const = matches!(
5556
hir.body_const_context(hir.body_owner_def_id(body.id())),
5657
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))

clippy_lints/src/no_effect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
2+
use clippy_utils::qualify_min_const_fn::is_inside_always_const_context;
23
use clippy_utils::source::snippet_opt;
34
use clippy_utils::ty::has_drop;
45
use clippy_utils::{any_parent_is_automatically_derived, is_lint_allowed, path_to_local, peel_blocks};

clippy_utils/src/qualify_min_const_fn.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_attr::StableSince;
99
use rustc_const_eval::transform::check_consts::ConstCx;
1010
use rustc_hir as hir;
1111
use rustc_hir::def_id::DefId;
12+
use rustc_hir::{ConstContext, HirId};
1213
use rustc_infer::infer::TyCtxtInferExt;
1314
use rustc_infer::traits::Obligation;
1415
use rustc_middle::mir::{
@@ -398,6 +399,18 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
398399
})
399400
}
400401

402+
pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
403+
use ConstContext::{Const, ConstFn, Static};
404+
let hir = tcx.hir();
405+
let Some(ctx) = hir.body_const_context(hir.enclosing_body_owner(hir_id)) else {
406+
return false;
407+
};
408+
match ctx {
409+
ConstFn => false,
410+
Static(_) | Const { inline: _ } => true,
411+
}
412+
}
413+
401414
fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
402415
// FIXME(effects, fee1-dead) revert to const destruct once it works again
403416
#[expect(unused)]

0 commit comments

Comments
 (0)