Skip to content

Commit eb68787

Browse files
authored
Rollup merge of #155507 - Kivooeo:no-expect, r=mejrs
suggest expect instead of unwrap when arg provided r? mejrs (feel free to reroll) fixes #155502
2 parents 1ef67b5 + e4c852d commit eb68787

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,17 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
23462346
provided_span,
23472347
format!("unexpected argument{idx}{provided_ty_name}"),
23482348
));
2349+
if self.provided_arg_tys.len() == 1
2350+
&& let Some(span) = self.maybe_suggest_expect_for_unwrap(provided_ty)
2351+
{
2352+
err.span_suggestion_verbose(
2353+
span,
2354+
"did you mean to use `expect`?",
2355+
"expect",
2356+
Applicability::MaybeIncorrect,
2357+
);
2358+
continue;
2359+
}
23492360
let mut span = provided_span;
23502361
if span.can_be_used_for_suggestions()
23512362
&& self.call_metadata.error_span.can_be_used_for_suggestions()
@@ -2776,6 +2787,22 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
27762787

27772788
(suggestion_span, suggestion)
27782789
}
2790+
2791+
fn maybe_suggest_expect_for_unwrap(&self, provided_ty: Ty<'tcx>) -> Option<Span> {
2792+
let tcx = self.tcx();
2793+
if let Some(call_ident) = self.call_metadata.call_ident
2794+
&& call_ident.name == sym::unwrap
2795+
&& let Some(callee_ty) = self.callee_ty
2796+
&& let ty::Adt(adt, _) = callee_ty.peel_refs().kind()
2797+
&& (tcx.is_diagnostic_item(sym::Option, adt.did())
2798+
|| tcx.is_diagnostic_item(sym::Result, adt.did()))
2799+
&& self.may_coerce(provided_ty, Ty::new_static_str(tcx))
2800+
{
2801+
Some(call_ident.span)
2802+
} else {
2803+
None
2804+
}
2805+
}
27792806
}
27802807

27812808
struct ArgMatchingCtxt<'a, 'b, 'tcx> {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
Ok(42).unwrap("wow");
3+
//~^ ERROR this method takes 0 arguments but 1 argument was supplied
4+
Some(42).unwrap("wow");
5+
//~^ ERROR this method takes 0 arguments but 1 argument was supplied
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0061]: this method takes 0 arguments but 1 argument was supplied
2+
--> $DIR/expect-instead-of-unwrap.rs:2:12
3+
|
4+
LL | Ok(42).unwrap("wow");
5+
| ^^^^^^ ----- unexpected argument of type `&'static str`
6+
|
7+
note: method defined here
8+
--> $SRC_DIR/core/src/result.rs:LL:COL
9+
help: did you mean to use `expect`?
10+
|
11+
LL - Ok(42).unwrap("wow");
12+
LL + Ok(42).expect("wow");
13+
|
14+
15+
error[E0061]: this method takes 0 arguments but 1 argument was supplied
16+
--> $DIR/expect-instead-of-unwrap.rs:4:14
17+
|
18+
LL | Some(42).unwrap("wow");
19+
| ^^^^^^ ----- unexpected argument of type `&'static str`
20+
|
21+
note: method defined here
22+
--> $SRC_DIR/core/src/option.rs:LL:COL
23+
help: did you mean to use `expect`?
24+
|
25+
LL - Some(42).unwrap("wow");
26+
LL + Some(42).expect("wow");
27+
|
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)