Create rule S7423: Unit values should not be compared (#4764)

* Create rule S7423

* 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:11 +00:00 committed by GitHub
parent e1ee16f500
commit 66bae183f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 0 deletions

View File

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

View File

@ -0,0 +1,24 @@
{
"title": "Unit values should not be compared",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7423",
"sqKey": "S7423",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,40 @@
== Why is this an issue?
The unit type `()` is always equal to itself and using it in comparisons is either redundant or a mistake, often caused by accidental semicolon placement.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
fn foo() {}
fn bar() {}
fn baz() {}
if {
foo();
} == {
bar();
} {
baz();
} // Noncompliant: Comparing unit values.
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
fn foo() {}
fn bar() {}
fn baz() {}
foo();
bar();
baz(); // Compliant: No unit comparison.
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp