Skip to content

Commit 4af94cf

Browse files
Suggest value on bare return
1 parent e4c71f1 commit 4af94cf

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use rustc_span::DesugaringKind;
6363
use rustc_span::{BytePos, Span};
6464
use rustc_target::spec::abi::Abi;
6565
use rustc_trait_selection::infer::InferCtxtExt as _;
66+
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
6667
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
6768
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
6869
use rustc_trait_selection::traits::TraitEngineExt as _;
@@ -1616,6 +1617,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16161617
E0069,
16171618
"`return;` in a function whose return type is not `()`"
16181619
);
1620+
if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
1621+
{
1622+
err.span_suggestion_verbose(
1623+
cause.span.shrink_to_hi(),
1624+
"give the `return` a value of the expected type",
1625+
format!(" {value}"),
1626+
Applicability::HasPlaceholders,
1627+
);
1628+
}
16191629
err.span_label(cause.span, "return type is not `()`");
16201630
}
16211631
ObligationCauseCode::BlockTailExpression(blk_id, ..) => {

tests/ui/error-codes/E0069.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | fn foo() -> u8 {
55
| -- expected `u8` because of this return type
66
LL | return;
77
| ^^^^^^ return type is not `()`
8+
|
9+
help: give the `return` a value of the expected type
10+
|
11+
LL | return 42;
12+
| ++
813

914
error: aborting due to 1 previous error
1015

tests/ui/ret-non-nil.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | fn g() -> isize { return; }
55
| ----- ^^^^^^ return type is not `()`
66
| |
77
| expected `isize` because of this return type
8+
|
9+
help: give the `return` a value of the expected type
10+
|
11+
LL | fn g() -> isize { return 42; }
12+
| ++
813

914
error: aborting due to 1 previous error
1015

tests/ui/return/suggest-a-value.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn test() -> (i32,) {
2+
return;
3+
//~^ ERROR `return;` in a function whose return type is not `()`
4+
}
5+
6+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0069]: `return;` in a function whose return type is not `()`
2+
--> $DIR/suggest-a-value.rs:2:5
3+
|
4+
LL | fn test() -> (i32,) {
5+
| ------ expected `(i32,)` because of this return type
6+
LL | return;
7+
| ^^^^^^ return type is not `()`
8+
|
9+
help: give the `return` a value of the expected type
10+
|
11+
LL | return (42);
12+
| ++++
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0069`.

0 commit comments

Comments
 (0)