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:
parent
ca818ac21b
commit
ec02230f32
2
rules/S7415/metadata.json
Normal file
2
rules/S7415/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7415/rust/metadata.json
Normal file
24
rules/S7415/rust/metadata.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
31
rules/S7415/rust/rule.adoc
Normal file
31
rules/S7415/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user