32 lines
759 B
Plaintext
32 lines
759 B
Plaintext
![]() |
== Why is this an issue?
|
||
|
|
||
|
Having an immutable condition in a while loop can result in an infinite loop as the condition will never change, causing the loop to run indefinitely.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||
|
----
|
||
|
let i = 0;
|
||
|
while i > 10 {
|
||
|
println!("let me loop forever!"); // Noncompliant: 'i' does not change within the loop body
|
||
|
}
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||
|
----
|
||
|
let mut i = 0;
|
||
|
while i <= 10 {
|
||
|
println!("looping: {}", i); // Compliant: 'i' gets incremented within the loop body
|
||
|
i += 1;
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Resources
|
||
|
=== Documentation
|
||
|
|
||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#while_immutable_condition
|