Skip to content

Commit 31c6064

Browse files
committed
Provide structured suggestion for type mismatch in loop
We currently provide only a `help` message, this PR introduces the last two structured suggestions instead: ``` 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 | | 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 | 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 | LL ~ } LL ~ /* `i32` value */ | help: otherwhise consider changing the return type to account for that possibility | LL ~ fn foo() -> Option<i32> { LL | for i in 0..0 { LL ~ return Some(i); LL ~ } LL ~ None | ``` Fix rust-lang#98982.
1 parent 27794f9 commit 31c6064

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

tests/ui/for-loop-while/break-while-condition.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ LL | while false {
3838
| ^^^^^^^^^^^ this might have zero elements to iterate on
3939
LL | return
4040
| ------ if the loop doesn't execute, this value would never get returned
41-
= 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
41+
= help: return a value for the case when the loop has zero elements to iterate on, otherwhise consider changing the return type to account for that possibility
4242

4343
error: aborting due to 3 previous errors
4444

tests/ui/typeck/issue-100285.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
fn foo(n: i32) -> i32 {
2-
for i in 0..0 {
3-
//~^ ERROR: mismatched types [E0308]
1+
fn foo(n: i32) -> i32 { //~ HELP otherwhise consider changing the return type to account for that possibility
2+
for i in 0..0 { //~ ERROR mismatched types [E0308]
43
if n < 0 {
54
return i;
65
} else if n < 10 {
@@ -15,8 +14,7 @@ fn foo(n: i32) -> i32 {
1514
return 5;
1615
}
1716

18-
}
19-
//~| 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
17+
} //~ HELP return a value for the case when the loop has zero elements to iterate on
2018
}
2119

2220
fn main() {}

tests/ui/typeck/issue-100285.stderr

+28-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ error[E0308]: mismatched types
44
LL | fn foo(n: i32) -> i32 {
55
| --- expected `i32` because of return type
66
LL | / for i in 0..0 {
7-
LL | |
87
LL | | if n < 0 {
98
LL | | return i;
9+
LL | | } else if n < 10 {
1010
... |
1111
LL | |
1212
LL | | }
@@ -17,7 +17,7 @@ note: the function expects a value to always be returned, but loops might run ze
1717
|
1818
LL | for i in 0..0 {
1919
| ^^^^^^^^^^^^^ this might have zero elements to iterate on
20-
...
20+
LL | if n < 0 {
2121
LL | return i;
2222
| -------- if the loop doesn't execute, this value would never get returned
2323
LL | } else if n < 10 {
@@ -27,7 +27,32 @@ LL | } else if n < 20 {
2727
LL | return 2;
2828
| -------- if the loop doesn't execute, this value would never get returned
2929
= note: if the loop doesn't execute, 3 other values would never get returned
30-
= 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
30+
help: return a value for the case when the loop has zero elements to iterate on
31+
|
32+
LL ~ }
33+
LL ~ /* `i32` value */
34+
|
35+
help: otherwhise consider changing the return type to account for that possibility
36+
|
37+
LL ~ fn foo(n: i32) -> Option<i32> {
38+
LL | for i in 0..0 {
39+
LL | if n < 0 {
40+
LL ~ return Some(i);
41+
LL | } else if n < 10 {
42+
LL ~ return Some(1);
43+
LL | } else if n < 20 {
44+
LL ~ return Some(2);
45+
LL | } else if n < 30 {
46+
LL ~ return Some(3);
47+
LL | } else if n < 40 {
48+
LL ~ return Some(4);
49+
LL | } else {
50+
LL ~ return Some(5);
51+
LL | }
52+
LL |
53+
LL ~ }
54+
LL ~ None
55+
|
3156

3257
error: aborting due to previous error
3358

tests/ui/typeck/issue-98982.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
fn foo() -> i32 {
2-
for i in 0..0 {
3-
//~^ ERROR: mismatched types [E0308]
1+
fn foo() -> i32 { //~ HELP otherwhise consider changing the return type to account for that possibility
2+
for i in 0..0 { //~ ERROR mismatched types [E0308]
43
return i;
5-
}
6-
//~| 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
4+
} //~ HELP return a value for the case when the loop has zero elements to iterate on
75
}
86

97
fn main() {}

tests/ui/typeck/issue-98982.stderr

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0308]: mismatched types
44
LL | fn foo() -> i32 {
55
| --- expected `i32` because of return type
66
LL | / for i in 0..0 {
7-
LL | |
87
LL | | return i;
98
LL | | }
109
| |_____^ expected `i32`, found `()`
@@ -14,10 +13,21 @@ note: the function expects a value to always be returned, but loops might run ze
1413
|
1514
LL | for i in 0..0 {
1615
| ^^^^^^^^^^^^^ this might have zero elements to iterate on
17-
LL |
1816
LL | return i;
1917
| -------- if the loop doesn't execute, this value would never get returned
20-
= 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
18+
help: return a value for the case when the loop has zero elements to iterate on
19+
|
20+
LL ~ }
21+
LL ~ /* `i32` value */
22+
|
23+
help: otherwhise consider changing the return type to account for that possibility
24+
|
25+
LL ~ fn foo() -> Option<i32> {
26+
LL | for i in 0..0 {
27+
LL ~ return Some(i);
28+
LL ~ }
29+
LL ~ None
30+
|
2131

2232
error: aborting due to previous error
2333

0 commit comments

Comments
 (0)