Skip to content

Commit

Permalink
Rollup merge of #102650 - Rageking8:slightly-improve-no-return-for-re…
Browse files Browse the repository at this point in the history
…turning-function-error, r=compiler-errors

Slightly improve no return for returning function error

Fixes #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.
  • Loading branch information
notriddle authored Oct 5, 2022
2 parents 4025e95 + 5ddaece commit 55ebb61
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
err.span_suggestion_short(
span_semi,
"remove this semicolon",
"remove this semicolon to return this value",
"",
Applicability::MachineApplicable,
);
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/block-result/consider-removing-last-semi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub fn f() -> String {
| implicitly returns `()` as its body has no tail or `return` expression
LL | 0u8;
LL | "bla".to_string();
| - help: remove this semicolon
| - help: remove this semicolon to return this value

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

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

error: aborting due to 3 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/block-result/issue-11714.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn blah() -> i32 {
| implicitly returns `()` as its body has no tail or `return` expression
...
LL | ;
| - help: remove this semicolon
| - help: remove this semicolon to return this value

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/block-result/issue-13428.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | fn bar() -> String {
| implicitly returns `()` as its body has no tail or `return` expression
LL | "foobar".to_string()
LL | ;
| - help: remove this semicolon
| - help: remove this semicolon to return this value

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn foo() -> i32 {
fn main() {
let _x: i32 = {
//~^ ERROR mismatched types
foo() //~ HELP remove this semicolon
foo() //~ HELP remove this semicolon to return this value
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn foo() -> i32 {
fn main() {
let _x: i32 = {
//~^ ERROR mismatched types
foo(); //~ HELP remove this semicolon
foo(); //~ HELP remove this semicolon to return this value
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let _x: i32 = {
| ___________________^
LL | |
LL | | foo();
| | - help: remove this semicolon
| | - help: remove this semicolon to return this value
LL | | };
| |_____^ expected `i32`, found `()`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn plus_one(x: i32) -> i32 {
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | x + 1;
| - help: remove this semicolon
| - help: remove this semicolon to return this value

error[E0308]: mismatched types
--> $DIR/coercion-missing-tail-expected-type.rs:8:13
Expand All @@ -16,7 +16,7 @@ LL | fn foo() -> Result<u8, u64> {
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | Ok(1);
| - help: remove this semicolon
| - help: remove this semicolon to return this value
|
= note: expected enum `Result<u8, u64>`
found unit type `()`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-6458-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn foo(b: bool) -> Result<bool,String> {
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | Err("bar".to_string());
| - help: remove this semicolon
| - help: remove this semicolon to return this value
|
= note: expected enum `Result<bool, String>`
found unit type `()`
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/liveness/liveness-return-last-stmt-semi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// regression test for #8005

macro_rules! test { () => { fn foo() -> i32 { 1; } } }
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/liveness/liveness-return-last-stmt-semi.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
error[E0308]: mismatched types
--> $DIR/liveness-return-last-stmt-semi.rs:7:19
--> $DIR/liveness-return-last-stmt-semi.rs:6:19
|
LL | fn no_return() -> i32 {}
| --------- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression

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

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

error[E0308]: mismatched types
--> $DIR/liveness-return-last-stmt-semi.rs:4:41
--> $DIR/liveness-return-last-stmt-semi.rs:3:41
|
LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } }
| --- ^^^ expected `i32`, found `()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn not_all_paths(a: &str) -> u32 {
| implicitly returns `()` as its body has no tail or `return` expression
...
LL | };
| - help: remove this semicolon
| - help: remove this semicolon to return this value

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

0 comments on commit 55ebb61

Please sign in to comment.