Skip to content

Commit 55ebb61

Browse files
authored
Rollup merge of rust-lang#102650 - Rageking8:slightly-improve-no-return-for-returning-function-error, r=compiler-errors
Slightly improve no return for returning function error Fixes rust-lang#100607 The rationale is that absolute beginners will be slightly confused as to why certain lines of code in a function does not require a semicolon. (I have actually witness a beginner having this confusion). Hence, a slight rationale is added "to return this value", which signals to the user that after removing said semicolon the value is returned resolving that error. However, if this is not desirable, I welcome any other suggestions. Thanks.
2 parents 4025e95 + 5ddaece commit 55ebb61

12 files changed

+18
-19
lines changed

compiler/rustc_hir_analysis/src/check/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11231123
} else {
11241124
err.span_suggestion_short(
11251125
span_semi,
1126-
"remove this semicolon",
1126+
"remove this semicolon to return this value",
11271127
"",
11281128
Applicability::MachineApplicable,
11291129
);

src/test/ui/block-result/consider-removing-last-semi.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub fn f() -> String {
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | 0u8;
99
LL | "bla".to_string();
10-
| - help: remove this semicolon
10+
| - help: remove this semicolon to return this value
1111

1212
error[E0308]: mismatched types
1313
--> $DIR/consider-removing-last-semi.rs:8:15
@@ -18,7 +18,7 @@ LL | pub fn g() -> String {
1818
| implicitly returns `()` as its body has no tail or `return` expression
1919
LL | "this won't work".to_string();
2020
LL | "removeme".to_string();
21-
| - help: remove this semicolon
21+
| - help: remove this semicolon to return this value
2222

2323
error[E0308]: mismatched types
2424
--> $DIR/consider-removing-last-semi.rs:13:25
@@ -29,7 +29,7 @@ LL | pub fn macro_tests() -> u32 {
2929
| implicitly returns `()` as its body has no tail or `return` expression
3030
...
3131
LL | mac!();
32-
| - help: remove this semicolon
32+
| - help: remove this semicolon to return this value
3333

3434
error: aborting due to 3 previous errors
3535

src/test/ui/block-result/issue-11714.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn blah() -> i32 {
77
| implicitly returns `()` as its body has no tail or `return` expression
88
...
99
LL | ;
10-
| - help: remove this semicolon
10+
| - help: remove this semicolon to return this value
1111

1212
error: aborting due to previous error
1313

src/test/ui/block-result/issue-13428.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | fn bar() -> String {
1515
| implicitly returns `()` as its body has no tail or `return` expression
1616
LL | "foobar".to_string()
1717
LL | ;
18-
| - help: remove this semicolon
18+
| - help: remove this semicolon to return this value
1919

2020
error: aborting due to 2 previous errors
2121

src/test/ui/closures/old-closure-expression-remove-semicolon.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn foo() -> i32 {
77
fn main() {
88
let _x: i32 = {
99
//~^ ERROR mismatched types
10-
foo() //~ HELP remove this semicolon
10+
foo() //~ HELP remove this semicolon to return this value
1111
};
1212
}

src/test/ui/closures/old-closure-expression-remove-semicolon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn foo() -> i32 {
77
fn main() {
88
let _x: i32 = {
99
//~^ ERROR mismatched types
10-
foo(); //~ HELP remove this semicolon
10+
foo(); //~ HELP remove this semicolon to return this value
1111
};
1212
}

src/test/ui/closures/old-closure-expression-remove-semicolon.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _x: i32 = {
55
| ___________________^
66
LL | |
77
LL | | foo();
8-
| | - help: remove this semicolon
8+
| | - help: remove this semicolon to return this value
99
LL | | };
1010
| |_____^ expected `i32`, found `()`
1111

src/test/ui/coercion/coercion-missing-tail-expected-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn plus_one(x: i32) -> i32 {
66
| |
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | x + 1;
9-
| - help: remove this semicolon
9+
| - help: remove this semicolon to return this value
1010

1111
error[E0308]: mismatched types
1212
--> $DIR/coercion-missing-tail-expected-type.rs:8:13
@@ -16,7 +16,7 @@ LL | fn foo() -> Result<u8, u64> {
1616
| |
1717
| implicitly returns `()` as its body has no tail or `return` expression
1818
LL | Ok(1);
19-
| - help: remove this semicolon
19+
| - help: remove this semicolon to return this value
2020
|
2121
= note: expected enum `Result<u8, u64>`
2222
found unit type `()`

src/test/ui/issues/issue-6458-4.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn foo(b: bool) -> Result<bool,String> {
66
| |
77
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | Err("bar".to_string());
9-
| - help: remove this semicolon
9+
| - help: remove this semicolon to return this value
1010
|
1111
= note: expected enum `Result<bool, String>`
1212
found unit type `()`

src/test/ui/liveness/liveness-return-last-stmt-semi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//
21
// regression test for #8005
32

43
macro_rules! test { () => { fn foo() -> i32 { 1; } } }

src/test/ui/liveness/liveness-return-last-stmt-semi.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error[E0308]: mismatched types
2-
--> $DIR/liveness-return-last-stmt-semi.rs:7:19
2+
--> $DIR/liveness-return-last-stmt-semi.rs:6:19
33
|
44
LL | fn no_return() -> i32 {}
55
| --------- ^^^ expected `i32`, found `()`
66
| |
77
| implicitly returns `()` as its body has no tail or `return` expression
88

99
error[E0308]: mismatched types
10-
--> $DIR/liveness-return-last-stmt-semi.rs:9:19
10+
--> $DIR/liveness-return-last-stmt-semi.rs:8:19
1111
|
1212
LL | fn bar(x: u32) -> u32 {
1313
| --- ^^^ expected `u32`, found `()`
1414
| |
1515
| implicitly returns `()` as its body has no tail or `return` expression
1616
LL | x * 2;
17-
| - help: remove this semicolon
17+
| - help: remove this semicolon to return this value
1818

1919
error[E0308]: mismatched types
20-
--> $DIR/liveness-return-last-stmt-semi.rs:13:19
20+
--> $DIR/liveness-return-last-stmt-semi.rs:12:19
2121
|
2222
LL | fn baz(x: u64) -> u32 {
2323
| --- ^^^ expected `u32`, found `()`
2424
| |
2525
| implicitly returns `()` as its body has no tail or `return` expression
2626

2727
error[E0308]: mismatched types
28-
--> $DIR/liveness-return-last-stmt-semi.rs:4:41
28+
--> $DIR/liveness-return-last-stmt-semi.rs:3:41
2929
|
3030
LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } }
3131
| --- ^^^ expected `i32`, found `()`

src/test/ui/suggestions/match-with-different-arm-types-as-stmt-instead-of-expr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn not_all_paths(a: &str) -> u32 {
77
| implicitly returns `()` as its body has no tail or `return` expression
88
...
99
LL | };
10-
| - help: remove this semicolon
10+
| - help: remove this semicolon to return this value
1111

1212
error[E0308]: `match` arms have incompatible types
1313
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:26:14

0 commit comments

Comments
 (0)