about summary refs log tree commit diff
path: root/tests/ui/for-loop-while
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-11-19 21:01:57 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-11-22 19:56:53 +0000
commit45efb8e6a68081462a70fa8cb40510d50ab848a4 (patch)
tree7b31a678fb172e6f6ad6f2809abd364e568fc3c6 /tests/ui/for-loop-while
parent73bc12199ea8c7651ed98b069c0dd6b0bb5fabcf (diff)
downloadrust-45efb8e6a68081462a70fa8cb40510d50ab848a4.tar.gz
rust-45efb8e6a68081462a70fa8cb40510d50ab848a4.zip
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
   |
```

Fix #98982.
Diffstat (limited to 'tests/ui/for-loop-while')
-rw-r--r--tests/ui/for-loop-while/break-while-condition.stderr2
1 files changed, 1 insertions, 1 deletions
diff --git a/tests/ui/for-loop-while/break-while-condition.stderr b/tests/ui/for-loop-while/break-while-condition.stderr
index e79f6a75fde..48b29f44fa1 100644
--- a/tests/ui/for-loop-while/break-while-condition.stderr
+++ b/tests/ui/for-loop-while/break-while-condition.stderr
@@ -38,7 +38,7 @@ LL |             while false {
    |             ^^^^^^^^^^^ this might have zero elements to iterate on
 LL |                 return
    |                 ------ 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
+   = 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
 
 error: aborting due to 3 previous errors