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:
github-actions[bot] 2025-03-19 13:06:43 +00:00 committed by GitHub
parent 6919fdfd79
commit 26f042cc83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 0 deletions

View File

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

View 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"
}
}

View 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