Skip to content
/ rust Public
forked from rust-lang/rust

Commit b0fa220

Browse files
committed
bless clippy
1 parent 2d59451 commit b0fa220

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2828
&& tcx.constness(parent_id) == hir::Constness::Const
2929
}
3030

31-
/// Checks whether an item is considered to be `const`. If it is a constructor, it is const. If
32-
/// it is a trait impl/function, return if it has a `const` modifier. If it is an intrinsic,
33-
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
34-
/// `Constness::NotConst`.
31+
/// Checks whether an item is considered to be `const`. If it is a constructor, anonymous const,
32+
/// const block, const item or associated const, it is const. If it is a trait impl/function,
33+
/// return if it has a `const` modifier. If it is an intrinsic, report whether said intrinsic
34+
/// has a `rustc_const_{un,}stable` attribute. Otherwise, return `Constness::NotConst`.
3535
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3636
let node = tcx.hir().get_by_def_id(def_id);
3737

3838
match node {
39-
hir::Node::Ctor(_) => hir::Constness::Const,
39+
hir::Node::Ctor(_)
40+
| hir::Node::AnonConst(_)
41+
| hir::Node::ConstBlock(_)
42+
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => hir::Constness::Const,
4043
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
4144
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
4245
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other

src/tools/clippy/clippy_lints/src/manual_float_methods.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::{is_from_proc_macro, path_to_local};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{BinOpKind, Expr, ExprKind};
6+
use rustc_hir::{BinOpKind, Constness, Expr, ExprKind};
77
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -83,8 +83,10 @@ impl Variant {
8383
impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
8484
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
8585
if !in_external_macro(cx.sess(), expr.span)
86-
&& cx.tcx.features().active(sym!(const_float_classify))
87-
&& let ExprKind::Binary(kind, lhs, rhs) = expr.kind
86+
&& (
87+
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
88+
|| cx.tcx.features().active(sym!(const_float_classify))
89+
) && let ExprKind::Binary(kind, lhs, rhs) = expr.kind
8890
&& let ExprKind::Binary(lhs_kind, lhs_lhs, lhs_rhs) = lhs.kind
8991
&& let ExprKind::Binary(rhs_kind, rhs_lhs, rhs_rhs) = rhs.kind
9092
// Checking all possible scenarios using a function would be a hopeless task, as we have

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -391,32 +391,38 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
391391

392392
#[expect(clippy::similar_names)] // bit too pedantic
393393
fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
394-
// Avoid selecting for simple cases, such as builtin types.
395-
if ty::util::is_trivially_const_drop(ty) {
396-
return true;
397-
}
394+
// FIXME(effects, fee1-dead) revert to const destruct once it works again
395+
#[expect(unused)]
396+
fn is_ty_const_destruct_unused<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
397+
// Avoid selecting for simple cases, such as builtin types.
398+
if ty::util::is_trivially_const_drop(ty) {
399+
return true;
400+
}
398401

399-
let obligation = Obligation::new(
400-
tcx,
401-
ObligationCause::dummy_with_span(body.span),
402-
ConstCx::new(tcx, body).param_env,
403-
TraitRef::from_lang_item(tcx, LangItem::Destruct, body.span, [ty]).with_constness(BoundConstness::ConstIfConst),
404-
);
402+
let obligation = Obligation::new(
403+
tcx,
404+
ObligationCause::dummy_with_span(body.span),
405+
ConstCx::new(tcx, body).param_env,
406+
TraitRef::from_lang_item(tcx, LangItem::Destruct, body.span, [ty]).with_constness(BoundConstness::ConstIfConst),
407+
);
405408

406-
let infcx = tcx.infer_ctxt().build();
407-
let mut selcx = SelectionContext::new(&infcx);
408-
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
409-
return false;
410-
};
409+
let infcx = tcx.infer_ctxt().build();
410+
let mut selcx = SelectionContext::new(&infcx);
411+
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
412+
return false;
413+
};
414+
415+
if !matches!(
416+
impl_src,
417+
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(ty::BoundConstness::ConstIfConst, _)
418+
) {
419+
return false;
420+
}
411421

412-
if !matches!(
413-
impl_src,
414-
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(ty::BoundConstness::ConstIfConst, _)
415-
) {
416-
return false;
422+
let ocx = ObligationCtxt::new(&infcx);
423+
ocx.register_obligations(impl_src.nested_obligations());
424+
ocx.select_all_or_error().is_empty()
417425
}
418426

419-
let ocx = ObligationCtxt::new(&infcx);
420-
ocx.register_obligations(impl_src.nested_obligations());
421-
ocx.select_all_or_error().is_empty()
427+
!ty.needs_drop(tcx, ConstCx::new(tcx, body).param_env)
422428
}

src/tools/clippy/tests/ui/missing_const_for_fn/could_be_const.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,5 @@ impl const Drop for D {
9999
}
100100

101101
// Lint this, since it can be dropped in const contexts
102+
// FIXME(effects)
102103
fn d(this: D) {}

src/tools/clippy/tests/ui/missing_const_for_fn/could_be_const.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,5 @@ LL | | 46
8989
LL | | }
9090
| |_^
9191

92-
error: this could be a `const fn`
93-
--> $DIR/could_be_const.rs:102:1
94-
|
95-
LL | fn d(this: D) {}
96-
| ^^^^^^^^^^^^^^^^
97-
98-
error: aborting due to 12 previous errors
92+
error: aborting due to 11 previous errors
9993

0 commit comments

Comments
 (0)