diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 720f4927ea8..079cca82408 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -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 _; @@ -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, ..) => { diff --git a/tests/ui/error-codes/E0069.stderr b/tests/ui/error-codes/E0069.stderr index 20ff8c258a0..ef6c411ae58 100644 --- a/tests/ui/error-codes/E0069.stderr +++ b/tests/ui/error-codes/E0069.stderr @@ -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 diff --git a/tests/ui/ret-non-nil.stderr b/tests/ui/ret-non-nil.stderr index 17567c6016a..802900e61a3 100644 --- a/tests/ui/ret-non-nil.stderr +++ b/tests/ui/ret-non-nil.stderr @@ -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 diff --git a/tests/ui/return/suggest-a-value.rs b/tests/ui/return/suggest-a-value.rs new file mode 100644 index 00000000000..7d23c0c44b7 --- /dev/null +++ b/tests/ui/return/suggest-a-value.rs @@ -0,0 +1,6 @@ +fn test() -> (i32,) { + return; + //~^ ERROR `return;` in a function whose return type is not `()` +} + +fn main() {} diff --git a/tests/ui/return/suggest-a-value.stderr b/tests/ui/return/suggest-a-value.stderr new file mode 100644 index 00000000000..3e0045a3ec4 --- /dev/null +++ b/tests/ui/return/suggest-a-value.stderr @@ -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`.