about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-04-14 09:35:52 -0400
committerMichael Goulet <michael@errs.io>2024-04-14 09:42:53 -0400
commit4af94cfa05993786653416571fdfa56c6f68c489 (patch)
tree6c2dafe64f357d57fcb88abe1b89391e61b4a37c
parente4c71f1fd8caf703dde7edb75b3cd33585aadddc (diff)
downloadrust-4af94cfa05993786653416571fdfa56c6f68c489.tar.gz
rust-4af94cfa05993786653416571fdfa56c6f68c489.zip
Suggest value on bare return
-rw-r--r--compiler/rustc_hir_typeck/src/coercion.rs10
-rw-r--r--tests/ui/error-codes/E0069.stderr5
-rw-r--r--tests/ui/ret-non-nil.stderr5
-rw-r--r--tests/ui/return/suggest-a-value.rs6
-rw-r--r--tests/ui/return/suggest-a-value.stderr16
5 files changed, 42 insertions, 0 deletions
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`.