Skip to content

Commit

Permalink
Suggest value on bare return
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 14, 2024
1 parent e4c71f1 commit 4af94cf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use rustc_span::DesugaringKind;
use rustc_span::{BytePos, Span};
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::infer::InferCtxtExt as _;
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits::TraitEngineExt as _;
Expand Down Expand Up @@ -1616,6 +1617,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
E0069,
"`return;` in a function whose return type is not `()`"
);
if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
{
err.span_suggestion_verbose(
cause.span.shrink_to_hi(),
"give the `return` a value of the expected type",
format!(" {value}"),
Applicability::HasPlaceholders,
);
}
err.span_label(cause.span, "return type is not `()`");
}
ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/error-codes/E0069.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ LL | fn foo() -> u8 {
| -- expected `u8` because of this return type
LL | return;
| ^^^^^^ return type is not `()`
|
help: give the `return` a value of the expected type
|
LL | return 42;
| ++

error: aborting due to 1 previous error

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/ret-non-nil.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ LL | fn g() -> isize { return; }
| ----- ^^^^^^ return type is not `()`
| |
| expected `isize` because of this return type
|
help: give the `return` a value of the expected type
|
LL | fn g() -> isize { return 42; }
| ++

error: aborting due to 1 previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/return/suggest-a-value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn test() -> (i32,) {
return;
//~^ ERROR `return;` in a function whose return type is not `()`
}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/return/suggest-a-value.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0069]: `return;` in a function whose return type is not `()`
--> $DIR/suggest-a-value.rs:2:5
|
LL | fn test() -> (i32,) {
| ------ expected `(i32,)` because of this return type
LL | return;
| ^^^^^^ return type is not `()`
|
help: give the `return` a value of the expected type
|
LL | return (42);
| ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0069`.

0 comments on commit 4af94cf

Please sign in to comment.