-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #100094 - lyming2007:issue-98982, r=estebank
Detect type mismatch due to loop that might never iterate When loop as tail expression causes a miss match type E0308 error, recursively get the return statement and add diagnostic information on it.
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
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
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,9 @@ | ||
fn foo() -> i32 { | ||
for i in 0..0 { | ||
//~^ ERROR: mismatched types [E0308] | ||
return i; | ||
} | ||
//~| help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility | ||
} | ||
|
||
fn main() {} |
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,24 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-98982.rs:2:5 | ||
| | ||
LL | fn foo() -> i32 { | ||
| --- expected `i32` because of return type | ||
LL | / for i in 0..0 { | ||
LL | | | ||
LL | | return i; | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
| | ||
note: the function expects a value to always be returned, but loops might run zero times | ||
--> $DIR/issue-98982.rs:2:5 | ||
| | ||
LL | for i in 0..0 { | ||
| ^^^^^^^^^^^^^ this might have zero elements to iterate on | ||
LL | | ||
LL | return i; | ||
| -------- if the loop doesn't execute, this value would never get returned | ||
= help: return a value for the case when the loop has zero elements to iterate on, or consider changing the return type to account for that possibility | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |