File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 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+
114fn main ( ) {
215 let _ = foo ( true ) ;
316}
417
518fn 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}
Original file line number Diff line number Diff line change 11error[E0308]: mismatched types
2- --> $DIR/tail-expr-as-potential-return.rs:7 :9
2+ --> $DIR/tail-expr-as-potential-return.rs:28 :9
33 |
44LL | / if x {
55LL | | Err(42)
66 | | ^^^^^^^ expected `()`, found enum `Result`
7+ LL | | //| HELP you might have meant to return this value
78LL | | }
89 | |_____- expected this to be `()`
910 |
@@ -14,6 +15,23 @@ help: you might have meant to return this value
1415LL | 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
1937For more information about this error, try `rustc --explain E0308`.
You can’t perform that action at this time.
0 commit comments