Skip to content

Commit ab7c446

Browse files
Add regression test for #92308
This amends off of an existing test introduced in #81769, if you think I should make a separate test I will.
1 parent eddf806 commit ab7c446

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1+
// > Suggest `return`ing tail expressions that match return type
2+
// >
3+
// > Some newcomers are confused by the behavior of tail expressions,
4+
// > interpreting that "leaving out the `;` makes it the return value".
5+
// > To help them go in the right direction, suggest using `return` instead
6+
// > when applicable.
7+
// (original commit description for this test)
8+
//
9+
// This test was amended to also serve as a regression test for #92308, where
10+
// this suggestion would not trigger with async functions.
11+
//
12+
// edition:2018
13+
114
fn main() {
215
let _ = foo(true);
316
}
417

518
fn foo(x: bool) -> Result<f64, i32> {
619
if x {
720
Err(42) //~ ERROR mismatched types
21+
//| HELP you might have meant to return this value
22+
}
23+
Ok(42.0)
24+
}
25+
26+
async fn bar(x: bool) -> Result<f64, i32> {
27+
if x {
28+
Err(42) //~ ERROR mismatched types
29+
//| HELP you might have meant to return this value
830
}
931
Ok(42.0)
1032
}

src/test/ui/return/tail-expr-as-potential-return.stderr

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
error[E0308]: mismatched types
2-
--> $DIR/tail-expr-as-potential-return.rs:7:9
2+
--> $DIR/tail-expr-as-potential-return.rs:28:9
33
|
44
LL | / if x {
55
LL | | Err(42)
66
| | ^^^^^^^ expected `()`, found enum `Result`
7+
LL | | //| HELP you might have meant to return this value
78
LL | | }
89
| |_____- expected this to be `()`
910
|
@@ -14,6 +15,23 @@ help: you might have meant to return this value
1415
LL | return Err(42);
1516
| ++++++ +
1617

17-
error: aborting due to previous error
18+
error[E0308]: mismatched types
19+
--> $DIR/tail-expr-as-potential-return.rs:20:9
20+
|
21+
LL | / if x {
22+
LL | | Err(42)
23+
| | ^^^^^^^ expected `()`, found enum `Result`
24+
LL | | //| HELP you might have meant to return this value
25+
LL | | }
26+
| |_____- expected this to be `()`
27+
|
28+
= note: expected unit type `()`
29+
found enum `Result<_, {integer}>`
30+
help: you might have meant to return this value
31+
|
32+
LL | return Err(42);
33+
| ++++++ +
34+
35+
error: aborting due to 2 previous errors
1836

1937
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)