rust/tests/ui/for-loop-while
Esteban Küber 45efb8e6a6 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.
2023-11-22 19:56:53 +00:00
..
auto-loop.rs
break-outside-loop.rs
break-outside-loop.stderr
break-value.rs
break-while-condition.rs
break-while-condition.stderr
break.rs
cleanup-rvalue-during-if-and-while.rs
for-destruct.rs
for-loop-goofiness.rs
for-loop-has-unit-body.rs
for-loop-into-iterator.rs
for-loop-lifetime-of-unbound-values.rs
for-loop-macro.rs
for-loop-mut-ref-element.rs
for-loop-no-std.rs
for-loop-panic.rs
for-loop-unconstrained-element-type-i32-fallback.rs
foreach-external-iterators-break.rs
foreach-external-iterators-hashmap-break-restart.rs
foreach-external-iterators-hashmap.rs
foreach-external-iterators-loop.rs
foreach-external-iterators-nested.rs
foreach-external-iterators.rs
foreach-nested.rs
foreach-put-structured.rs
foreach-simple-outer-slot.rs
issue-1257.rs
issue-2216.rs
issue-51345.rs
issue-69841.rs
label_break_value_invalid.rs
label_break_value_invalid.stderr
label_break_value.rs
labeled-break.rs
linear-for-loop.rs
liveness-assign-imm-local-after-loop.rs
liveness-loop-break.rs
liveness-move-in-loop.rs
long-while.rs
loop-break-cont-1.rs
loop-break-cont.rs
loop-break-value.rs
loop-diverges.rs
loop-label-shadowing.rs
loop-labeled-break-value.rs
loop-no-reinit-needed-post-bot.rs
loop-scope.rs
while-cont.rs
while-flow-graph.rs
while-label.rs
while-let-2.rs
while-let-2.stderr
while-let.rs
while-loop-constraints-2.rs
while-prelude-drop.rs
while-with-break.rs
while.rs