rspec/rules/S7415/rust/rule.adoc
github-actions[bot] ec02230f32
Create rule S7415: Immutable variables should not be used in while loop conditions (#4754)
* Create rule S7415

* Update RSPEC

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
2025-03-19 13:07:33 +00:00

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