-
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.
when there are 3 or more return statements in the loop
emit the first 3 errors and duplicated diagnostic information using take of iterator for the first third return modified: compiler/rustc_typeck/src/check/coercion.rs new file: src/test/ui/typeck/issue-100285.rs new file: src/test/ui/typeck/issue-100285.stderr
- Loading branch information
Yiming Lei
committed
Aug 15, 2022
1 parent
6d3f1be
commit 0471e27
Showing
3 changed files
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
fn foo(n: i32) -> i32 { | ||
for i in 0..0 { | ||
//~^ ERROR: mismatched types [E0308] | ||
if n < 0 { | ||
return i; | ||
} else if n < 10 { | ||
return 1; | ||
} else if n < 20 { | ||
return 2; | ||
} else if n < 30 { | ||
return 3; | ||
} else if n < 40 { | ||
return 4; | ||
} else { | ||
return 5; | ||
} | ||
|
||
} | ||
//~| 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,34 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-100285.rs:2:5 | ||
| | ||
LL | fn foo(n: i32) -> i32 { | ||
| --- expected `i32` because of return type | ||
LL | / for i in 0..0 { | ||
LL | | | ||
LL | | if n < 0 { | ||
LL | | return i; | ||
... | | ||
LL | | | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
| | ||
note: the function expects a value to always be returned, but loops might run zero times | ||
--> $DIR/issue-100285.rs:2:5 | ||
| | ||
LL | for i in 0..0 { | ||
| ^^^^^^^^^^^^^ this might have zero elements to iterate on | ||
... | ||
LL | return i; | ||
| -------- if the loop doesn't execute, this value would never get returned | ||
LL | } else if n < 10 { | ||
LL | return 1; | ||
| -------- if the loop doesn't execute, this value would never get returned | ||
LL | } else if n < 20 { | ||
LL | return 2; | ||
| -------- if the loop doesn't execute, this value would never get returned | ||
= note: if the loop doesn't execute, 3 other values 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`. |