diff options
| author | Alejandra González <blyxyas@gmail.com> | 2025-05-10 21:15:31 +0000 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2025-06-20 10:01:19 -0700 |
| commit | 124ec117a9bb1d7eac367196bba3ba02d60f4834 (patch) | |
| tree | da9e6bd88955c0ba18fd96b198e3b50dd102cb0f | |
| parent | 2ec328fec2f8f61bda145b97283683ca8b801fc7 (diff) | |
| download | rust-124ec117a9bb1d7eac367196bba3ba02d60f4834.tar.gz rust-124ec117a9bb1d7eac367196bba3ba02d60f4834.zip | |
Fix ICE in `missing_const_for_fn` (rust-lang/rust-clippy#14776)
The `mir_drops_elaborated_and_const_checked` query result has been stolen already and cannot be borrowed again. Use the `optimized_mir` query result instead. changelog: [`missing_const_for_fn`]: fix ICE with some compilation options Fixes rust-lang/rust-clippy#14774 r? @Jarcho
4 files changed, 45 insertions, 2 deletions
diff --git a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs index 1f142bc3ba6..f3e24044fb6 100644 --- a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs +++ b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs @@ -155,9 +155,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { return; } - let mir = cx.tcx.mir_drops_elaborated_and_const_checked(def_id); + let mir = cx.tcx.optimized_mir(def_id); - if let Ok(()) = is_min_const_fn(cx, &mir.borrow(), self.msrv) + if let Ok(()) = is_min_const_fn(cx, mir, self.msrv) && let hir::Node::Item(hir::Item { vis_span, .. }) | hir::Node::ImplItem(hir::ImplItem { vis_span, .. }) = cx.tcx.hir_node_by_def_id(def_id) { diff --git a/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.fixed b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.fixed new file mode 100644 index 00000000000..9c85c4b8464 --- /dev/null +++ b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.fixed @@ -0,0 +1,13 @@ +//@compile-flags: -Z validate-mir +#![warn(clippy::missing_const_for_fn)] + +static BLOCK_FN_DEF: fn(usize) -> usize = { + //~v missing_const_for_fn + const fn foo(a: usize) -> usize { + a + 10 + } + foo +}; +struct X; + +fn main() {} diff --git a/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.rs b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.rs new file mode 100644 index 00000000000..6519be61256 --- /dev/null +++ b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.rs @@ -0,0 +1,13 @@ +//@compile-flags: -Z validate-mir +#![warn(clippy::missing_const_for_fn)] + +static BLOCK_FN_DEF: fn(usize) -> usize = { + //~v missing_const_for_fn + fn foo(a: usize) -> usize { + a + 10 + } + foo +}; +struct X; + +fn main() {} diff --git a/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.stderr b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.stderr new file mode 100644 index 00000000000..a407376d0b9 --- /dev/null +++ b/src/tools/clippy/tests/ui/crashes/missing_const_for_fn_14774.stderr @@ -0,0 +1,17 @@ +error: this could be a `const fn` + --> tests/ui/crashes/missing_const_for_fn_14774.rs:6:5 + | +LL | / fn foo(a: usize) -> usize { +LL | | a + 10 +LL | | } + | |_____^ + | + = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::missing_const_for_fn)]` +help: make the function `const` + | +LL | const fn foo(a: usize) -> usize { + | +++++ + +error: aborting due to 1 previous error + |
