add suggestion for expect

This commit is contained in:
Kivooeo
2026-04-19 15:04:06 +11:00
parent 0febdbab27
commit e4c852dece
3 changed files with 64 additions and 0 deletions
@@ -2342,6 +2342,17 @@ fn has_error_or_infer<'tcx>(tys: impl IntoIterator<Item = Ty<'tcx>>) -> bool {
provided_span,
format!("unexpected argument{idx}{provided_ty_name}"),
));
if self.provided_arg_tys.len() == 1
&& let Some(span) = self.maybe_suggest_expect_for_unwrap(provided_ty)
{
err.span_suggestion_verbose(
span,
"did you mean to use `expect`?",
"expect",
Applicability::MaybeIncorrect,
);
continue;
}
let mut span = provided_span;
if span.can_be_used_for_suggestions()
&& self.call_metadata.error_span.can_be_used_for_suggestions()
@@ -2772,6 +2783,22 @@ fn suggestion_code(&self) -> (Span, String) {
(suggestion_span, suggestion)
}
fn maybe_suggest_expect_for_unwrap(&self, provided_ty: Ty<'tcx>) -> Option<Span> {
let tcx = self.tcx();
if let Some(call_ident) = self.call_metadata.call_ident
&& call_ident.name == sym::unwrap
&& let Some(callee_ty) = self.callee_ty
&& let ty::Adt(adt, _) = callee_ty.peel_refs().kind()
&& (tcx.is_diagnostic_item(sym::Option, adt.did())
|| tcx.is_diagnostic_item(sym::Result, adt.did()))
&& self.may_coerce(provided_ty, Ty::new_static_str(tcx))
{
Some(call_ident.span)
} else {
None
}
}
}
struct ArgMatchingCtxt<'a, 'b, 'tcx> {
@@ -0,0 +1,6 @@
fn main() {
Ok(42).unwrap("wow");
//~^ ERROR this method takes 0 arguments but 1 argument was supplied
Some(42).unwrap("wow");
//~^ ERROR this method takes 0 arguments but 1 argument was supplied
}
@@ -0,0 +1,31 @@
error[E0061]: this method takes 0 arguments but 1 argument was supplied
--> $DIR/expect-instead-of-unwrap.rs:2:12
|
LL | Ok(42).unwrap("wow");
| ^^^^^^ ----- unexpected argument of type `&'static str`
|
note: method defined here
--> $SRC_DIR/core/src/result.rs:LL:COL
help: did you mean to use `expect`?
|
LL - Ok(42).unwrap("wow");
LL + Ok(42).expect("wow");
|
error[E0061]: this method takes 0 arguments but 1 argument was supplied
--> $DIR/expect-instead-of-unwrap.rs:4:14
|
LL | Some(42).unwrap("wow");
| ^^^^^^ ----- unexpected argument of type `&'static str`
|
note: method defined here
--> $SRC_DIR/core/src/option.rs:LL:COL
help: did you mean to use `expect`?
|
LL - Some(42).unwrap("wow");
LL + Some(42).expect("wow");
|
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0061`.