From 66bae183f4499bdd7073a38a176a35b7eff0da02 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 13:07:11 +0000 Subject: [PATCH] Create rule S7423: Unit values should not be compared (#4764) * Create rule S7423 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource Co-authored-by: yassin-kammoun-sonarsource --- rules/S7423/metadata.json | 2 ++ rules/S7423/rust/metadata.json | 24 ++++++++++++++++++++ rules/S7423/rust/rule.adoc | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 rules/S7423/metadata.json create mode 100644 rules/S7423/rust/metadata.json create mode 100644 rules/S7423/rust/rule.adoc diff --git a/rules/S7423/metadata.json b/rules/S7423/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7423/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7423/rust/metadata.json b/rules/S7423/rust/metadata.json new file mode 100644 index 0000000000..b98f438cfa --- /dev/null +++ b/rules/S7423/rust/metadata.json @@ -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" + } +} diff --git a/rules/S7423/rust/rule.adoc b/rules/S7423/rust/rule.adoc new file mode 100644 index 0000000000..39d75b69b2 --- /dev/null +++ b/rules/S7423/rust/rule.adoc @@ -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