File tree 2 files changed +42
-2
lines changed
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
+
1
14
fn main ( ) {
2
15
let _ = foo ( true ) ;
3
16
}
4
17
5
18
fn foo ( x : bool ) -> Result < f64 , i32 > {
6
19
if x {
7
20
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
8
30
}
9
31
Ok ( 42.0 )
10
32
}
Original file line number Diff line number Diff line change 1
1
error[E0308]: mismatched types
2
- --> $DIR/tail-expr-as-potential-return.rs:7 :9
2
+ --> $DIR/tail-expr-as-potential-return.rs:28 :9
3
3
|
4
4
LL | / if x {
5
5
LL | | Err(42)
6
6
| | ^^^^^^^ expected `()`, found enum `Result`
7
+ LL | | //| HELP you might have meant to return this value
7
8
LL | | }
8
9
| |_____- expected this to be `()`
9
10
|
@@ -14,6 +15,23 @@ help: you might have meant to return this value
14
15
LL | return Err(42);
15
16
| ++++++ +
16
17
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
18
36
19
37
For more information about this error, try `rustc --explain E0308`.
You can’t perform that action at this time.
0 commit comments