You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: otherwise 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
|
```
Fixrust-lang#98982.
Copy file name to clipboardExpand all lines: tests/ui/for-loop-while/break-while-condition.stderr
+1-1
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ LL | while false {
38
38
| ^^^^^^^^^^^ this might have zero elements to iterate on
39
39
LL | return
40
40
| ------ 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, otherwise consider changing the return type to account for that possibility
Copy file name to clipboardExpand all lines: tests/ui/typeck/issue-100285.rs
+3-5
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
1
-
fnfoo(n:i32) -> i32{
2
-
for i in0..0{
3
-
//~^ ERROR: mismatched types [E0308]
1
+
fnfoo(n:i32) -> i32{//~ HELP otherwise consider changing the return type to account for that possibility
2
+
for i in0..0{//~ ERROR mismatched types [E0308]
4
3
if n < 0{
5
4
return i;
6
5
}elseif n < 10{
@@ -15,8 +14,7 @@ fn foo(n: i32) -> i32 {
15
14
return5;
16
15
}
17
16
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
Copy file name to clipboardExpand all lines: tests/ui/typeck/issue-100285.stderr
+28-3
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ error[E0308]: mismatched types
4
4
LL | fn foo(n: i32) -> i32 {
5
5
| --- expected `i32` because of return type
6
6
LL | / for i in 0..0 {
7
-
LL | |
8
7
LL | | if n < 0 {
9
8
LL | | return i;
9
+
LL | | } else if n < 10 {
10
10
... |
11
11
LL | |
12
12
LL | | }
@@ -17,7 +17,7 @@ note: the function expects a value to always be returned, but loops might run ze
17
17
|
18
18
LL | for i in 0..0 {
19
19
| ^^^^^^^^^^^^^ this might have zero elements to iterate on
20
-
...
20
+
LL | if n < 0 {
21
21
LL | return i;
22
22
| -------- if the loop doesn't execute, this value would never get returned
23
23
LL | } else if n < 10 {
@@ -27,7 +27,32 @@ LL | } else if n < 20 {
27
27
LL | return 2;
28
28
| -------- if the loop doesn't execute, this value would never get returned
29
29
= 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: otherwise consider changing the return type to account for that possibility
fnfoo() -> i32{//~ HELP otherwise consider changing the return type to account for that possibility
2
+
for i in0..0{//~ ERROR mismatched types [E0308]
4
3
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
Copy file name to clipboardExpand all lines: tests/ui/typeck/issue-98982.stderr
+13-3
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,6 @@ error[E0308]: mismatched types
4
4
LL | fn foo() -> i32 {
5
5
| --- expected `i32` because of return type
6
6
LL | / for i in 0..0 {
7
-
LL | |
8
7
LL | | return i;
9
8
LL | | }
10
9
| |_____^ expected `i32`, found `()`
@@ -14,10 +13,21 @@ note: the function expects a value to always be returned, but loops might run ze
14
13
|
15
14
LL | for i in 0..0 {
16
15
| ^^^^^^^^^^^^^ this might have zero elements to iterate on
17
-
LL |
18
16
LL | return i;
19
17
| -------- 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: otherwise consider changing the return type to account for that possibility
0 commit comments