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>
This commit is contained in:
github-actions[bot] 2025-03-19 13:07:33 +00:00 committed by GitHub
parent ca818ac21b
commit ec02230f32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "Immutable variables should not be used in while loop conditions",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7415",
"sqKey": "S7415",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "HIGH"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,31 @@
== 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