Create rule S7436: Redundant comparisons should be removed (#4779)
* Create rule S7436 * 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
6919fdfd79
commit
26f042cc83
2
rules/S7436/metadata.json
Normal file
2
rules/S7436/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7436/rust/metadata.json
Normal file
24
rules/S7436/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "Redundant comparisons should be removed",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7436",
|
||||
"sqKey": "S7436",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
26
rules/S7436/rust/rule.adoc
Normal file
26
rules/S7436/rust/rule.adoc
Normal file
@ -0,0 +1,26 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Redundant comparisons against constants results in always-true or always-false conditions. They add unnecessary complexity to the code and often indicate a logical error where the developer might have intended a different comparison operator or a comparison with another value.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let status_code = 200;
|
||||
if status_code <= 400 && status_code < 500 {} // Noncompliant: Redundant double comparison.
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let status_code = 200;
|
||||
if status_code < 500 {} // Compliant: Corrected the comparison logic.
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#redundant_comparisons
|
Loading…
x
Reference in New Issue
Block a user