-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #58981 - estebank:elseless-if, r=davidtwco
Point at coercion reason for `if` expressions without else clause if caused by return type ``` error[E0317]: if may be missing an else clause --> $DIR/if-without-else-as-fn-expr.rs:2:5 | LL | fn foo(bar: usize) -> usize { | ----- found `usize` because of this return type LL | / if bar % 5 == 0 { LL | | return 3; LL | | } | |_____^ expected (), found usize | = note: expected type `()` found type `usize` = note: `if` expressions without `else` must evaluate to `()` ``` Fix #25228.
- Loading branch information
Showing
6 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
fn foo(bar: usize) -> usize { | ||
if bar % 5 == 0 { | ||
return 3; | ||
} | ||
//~^^^ ERROR if may be missing an else clause | ||
} | ||
|
||
fn foo2(bar: usize) -> usize { | ||
let x: usize = if bar % 5 == 0 { | ||
return 3; | ||
}; | ||
//~^^^ ERROR if may be missing an else clause | ||
x | ||
} | ||
|
||
fn foo3(bar: usize) -> usize { | ||
if bar % 5 == 0 { | ||
3 | ||
} | ||
//~^^^ ERROR if may be missing an else clause | ||
} | ||
|
||
fn main() { | ||
let _ = foo(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
error[E0317]: if may be missing an else clause | ||
--> $DIR/if-without-else-as-fn-expr.rs:2:5 | ||
| | ||
LL | fn foo(bar: usize) -> usize { | ||
| ----- expected `usize` because of this return type | ||
LL | / if bar % 5 == 0 { | ||
LL | | return 3; | ||
LL | | } | ||
| |_____^ expected usize, found () | ||
| | ||
= note: expected type `usize` | ||
found type `()` | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
|
||
error[E0317]: if may be missing an else clause | ||
--> $DIR/if-without-else-as-fn-expr.rs:9:20 | ||
| | ||
LL | let x: usize = if bar % 5 == 0 { | ||
| _________-__________^ | ||
| | | | ||
| | expected because of this assignment | ||
LL | | return 3; | ||
LL | | }; | ||
| |_____^ expected usize, found () | ||
| | ||
= note: expected type `usize` | ||
found type `()` | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
|
||
error[E0317]: if may be missing an else clause | ||
--> $DIR/if-without-else-as-fn-expr.rs:17:5 | ||
| | ||
LL | fn foo3(bar: usize) -> usize { | ||
| ----- expected `usize` because of this return type | ||
LL | / if bar % 5 == 0 { | ||
LL | | 3 | ||
LL | | } | ||
| |_____^ expected usize, found () | ||
| | ||
= note: expected type `usize` | ||
found type `()` | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0317`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters