Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
21 changes: 12 additions & 9 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/consts/const-result-no-expect-suggestion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fn f(value: u32) -> Result<u32, ()> {
Ok(value)
}

const TEST: u32 = f(2);
//~^ ERROR: mismatched types

const fn g() -> Result<String, ()> {
Ok(String::new())
}

const TEST2: usize = g().len();
//~^ ERROR: no method named `len` found for enum `Result<T, E>`

fn main() {}
24 changes: 24 additions & 0 deletions tests/ui/consts/const-result-no-expect-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -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<u32, ()>`
|
= note: expected type `u32`
found enum `Result<u32, ()>`

error[E0599]: no method named `len` found for enum `Result<T, E>` 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`.
Loading