From 80d7968f3dd47403a53b03cb904c24c197195a8f Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Wed, 26 Nov 2025 15:45:08 +0900 Subject: [PATCH] Don't suggest unwrap for Result in const --- .../src/fn_ctxt/suggestions.rs | 5 ++++ .../rustc_hir_typeck/src/method/suggest.rs | 21 +++++++++------- .../const-result-no-expect-suggestion.rs | 15 ++++++++++++ .../const-result-no-expect-suggestion.stderr | 24 +++++++++++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 tests/ui/consts/const-result-no-expect-suggestion.rs create mode 100644 tests/ui/consts/const-result-no-expect-suggestion.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 1d16c3af7fb75..5b63258dfa6b1 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2127,6 +2127,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "?", ) } else { + // Do not suggest `.expect()` in const context where it's not available. + if self.tcx.hir_is_inside_const_context(expr.hir_id) { + return false; + } + ( format!( "consider using `{kind}::expect` to unwrap the `{found}` value, \ diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 9a657ab159035..ff025b0997d65 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -3060,15 +3060,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Applicability::MachineApplicable, ); } else { - err.span_suggestion_verbose( - expr.span.shrink_to_hi(), - format!( - "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \ - panicking if the value is {article} `{kind}::{variant}`" - ), - ".expect(\"REASON\")", - Applicability::HasPlaceholders, - ); + // Do not suggest `.expect()` in const context where it's not available. + if !tcx.hir_is_inside_const_context(expr.hir_id) { + err.span_suggestion_verbose( + expr.span.shrink_to_hi(), + format!( + "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \ + panicking if the value is {article} `{kind}::{variant}`" + ), + ".expect(\"REASON\")", + Applicability::HasPlaceholders, + ); + } } } // FIXME(compiler-errors): Support suggestions for other matching enum variants diff --git a/tests/ui/consts/const-result-no-expect-suggestion.rs b/tests/ui/consts/const-result-no-expect-suggestion.rs new file mode 100644 index 0000000000000..cd725d9cee0ec --- /dev/null +++ b/tests/ui/consts/const-result-no-expect-suggestion.rs @@ -0,0 +1,15 @@ +const fn f(value: u32) -> Result { + Ok(value) +} + +const TEST: u32 = f(2); +//~^ ERROR: mismatched types + +const fn g() -> Result { + Ok(String::new()) +} + +const TEST2: usize = g().len(); +//~^ ERROR: no method named `len` found for enum `Result` + +fn main() {} diff --git a/tests/ui/consts/const-result-no-expect-suggestion.stderr b/tests/ui/consts/const-result-no-expect-suggestion.stderr new file mode 100644 index 0000000000000..70aa306ae3c88 --- /dev/null +++ b/tests/ui/consts/const-result-no-expect-suggestion.stderr @@ -0,0 +1,24 @@ +error[E0308]: mismatched types + --> $DIR/const-result-no-expect-suggestion.rs:5:19 + | +LL | const TEST: u32 = f(2); + | ^^^^ expected `u32`, found `Result` + | + = note: expected type `u32` + found enum `Result` + +error[E0599]: no method named `len` found for enum `Result` in the current scope + --> $DIR/const-result-no-expect-suggestion.rs:12:26 + | +LL | const TEST2: usize = g().len(); + | ^^^ + | +note: the method `len` exists on the type `String` + --> $SRC_DIR/alloc/src/string.rs:LL:COL +help: there is a method `le` with a similar name, but with different arguments + --> $SRC_DIR/core/src/cmp.rs:LL:COL + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0308, E0599. +For more information about an error, try `rustc --explain E0308`.