Skip to content

Commit ef8cc30

Browse files
committed
Don't suggest unwrap for Result in const
1 parent 80d8f29 commit ef8cc30

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21272127
"?",
21282128
)
21292129
} else {
2130+
// Do not suggest `.expect()` in const context where it's not available.
2131+
if self.tcx.hir_is_inside_const_context(expr.hir_id) {
2132+
return false;
2133+
}
2134+
21302135
(
21312136
format!(
21322137
"consider using `{kind}::expect` to unwrap the `{found}` value, \

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,15 +3060,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30603060
Applicability::MachineApplicable,
30613061
);
30623062
} else {
3063-
err.span_suggestion_verbose(
3064-
expr.span.shrink_to_hi(),
3065-
format!(
3066-
"consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
3067-
panicking if the value is {article} `{kind}::{variant}`"
3068-
),
3069-
".expect(\"REASON\")",
3070-
Applicability::HasPlaceholders,
3071-
);
3063+
// Do not suggest `.expect()` in const context where it's not available.
3064+
if !tcx.hir_is_inside_const_context(expr.hir_id) {
3065+
err.span_suggestion_verbose(
3066+
expr.span.shrink_to_hi(),
3067+
format!(
3068+
"consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
3069+
panicking if the value is {article} `{kind}::{variant}`"
3070+
),
3071+
".expect(\"REASON\")",
3072+
Applicability::HasPlaceholders,
3073+
);
3074+
}
30723075
}
30733076
}
30743077
// FIXME(compiler-errors): Support suggestions for other matching enum variants

tests/ui/consts/const-fn-unwrap.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fn f(value: u32) -> Result<u32, ()> {
2+
Ok(value)
3+
}
4+
5+
const TEST: u32 = f(2);
6+
//~^ ERROR: mismatched types
7+
8+
const fn g() -> Result<String, ()> {
9+
Ok(String::new())
10+
}
11+
12+
const TEST2: usize = g().len();
13+
//~^ ERROR: no method named `len` found for enum `Result<T, E>`
14+
15+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/const-fn-unwrap.rs:5:19
3+
|
4+
LL | const TEST: u32 = f(2);
5+
| ^^^^ expected `u32`, found `Result<u32, ()>`
6+
|
7+
= note: expected type `u32`
8+
found enum `Result<u32, ()>`
9+
10+
error[E0599]: no method named `len` found for enum `Result<T, E>` in the current scope
11+
--> $DIR/const-fn-unwrap.rs:12:26
12+
|
13+
LL | const TEST2: usize = g().len();
14+
| ^^^
15+
|
16+
note: the method `len` exists on the type `String`
17+
--> $SRC_DIR/alloc/src/string.rs:LL:COL
18+
help: there is a method `le` with a similar name, but with different arguments
19+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors have detailed explanations: E0308, E0599.
24+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)