W0009
WarningSafe fix
while with the condition true
WarningSafe fixA while whose condition is the literal true, with or without parentheses, is a warning, and the program still builds. The fix writes loop in its place and keeps the loop label. The fix is withheld when a jump or loop label in the loop failed, as with a break with a value (E0141): the rewrite could remove that error rather than fix it.
Why
loop says the same thing, an endless loop, and it is the one spelling for it. The two lower to the same code.
Broken
fn main() {
let mut n = 0
while true {
n += 1
if n == 3 {
break
}
}
print(n)
}
What sisu says
warning[W0009]: this `while` has the condition `true` --> W0009.sisu:3:5 | 3 | while true { | ^^^^^^^^^^ the condition never ends the loop | = help: write an endless loop as `loop` help: write `loop` | 3 - while true { 3 + loop { | sisu: 1 warning sisu: for more on a code, run `sisu explain <code>`: W0009
Fixed
fn main() {
let mut n = 0
loop {
n += 1
if n == 3 {
break
}
}
print(n)
}
Related
- E0141:
breakwith a value in a loop that has none.
Sisu