Skip to content

Commit

Permalink
Add regression test for #92308
Browse files Browse the repository at this point in the history
This amends off of an existing test introduced in #81769, if you think I
should make a separate test I will.
  • Loading branch information
ThePuzzlemaker committed Dec 29, 2021
1 parent eddf806 commit ab7c446
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/test/ui/return/tail-expr-as-potential-return.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
// > Suggest `return`ing tail expressions that match return type
// >
// > Some newcomers are confused by the behavior of tail expressions,
// > interpreting that "leaving out the `;` makes it the return value".
// > To help them go in the right direction, suggest using `return` instead
// > when applicable.
// (original commit description for this test)
//
// This test was amended to also serve as a regression test for #92308, where
// this suggestion would not trigger with async functions.
//
// edition:2018

fn main() {
let _ = foo(true);
}

fn foo(x: bool) -> Result<f64, i32> {
if x {
Err(42) //~ ERROR mismatched types
//| HELP you might have meant to return this value
}
Ok(42.0)
}

async fn bar(x: bool) -> Result<f64, i32> {
if x {
Err(42) //~ ERROR mismatched types
//| HELP you might have meant to return this value
}
Ok(42.0)
}
22 changes: 20 additions & 2 deletions src/test/ui/return/tail-expr-as-potential-return.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:7:9
--> $DIR/tail-expr-as-potential-return.rs:28:9
|
LL | / if x {
LL | | Err(42)
| | ^^^^^^^ expected `()`, found enum `Result`
LL | | //| HELP you might have meant to return this value
LL | | }
| |_____- expected this to be `()`
|
Expand All @@ -14,6 +15,23 @@ help: you might have meant to return this value
LL | return Err(42);
| ++++++ +

error: aborting due to previous error
error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:20:9
|
LL | / if x {
LL | | Err(42)
| | ^^^^^^^ expected `()`, found enum `Result`
LL | | //| HELP you might have meant to return this value
LL | | }
| |_____- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
help: you might have meant to return this value
|
LL | return Err(42);
| ++++++ +

error: aborting due to 2 previous errors

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

0 comments on commit ab7c446

Please sign in to comment.