diff --git a/rules/S7422/metadata.json b/rules/S7422/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7422/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7422/rust/metadata.json b/rules/S7422/rust/metadata.json new file mode 100644 index 0000000000..5e9db195ac --- /dev/null +++ b/rules/S7422/rust/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "Unit values should not be hashed", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clippy" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7422", + "sqKey": "S7422", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "HIGH" + }, + "attribute": "LOGICAL" + } +} diff --git a/rules/S7422/rust/rule.adoc b/rules/S7422/rust/rule.adoc new file mode 100644 index 0000000000..575fd2ff38 --- /dev/null +++ b/rules/S7422/rust/rule.adoc @@ -0,0 +1,42 @@ +== Why is this an issue? + +Hashing a unit value doesn't accomplish anything because the implementation of `Hash` for `()` is a no-op. This can lead to confusion or misleading code, as it implies some hashing operation is actually taking place, when in reality it does nothing. + +=== Code examples + +==== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +enum Foo { Empty, WithValue(u8) } +use Foo::*; + +let mut state = DefaultHasher::new(); +let my_enum = Foo::Empty; + +match my_enum { + Empty => ().hash(&mut state), // Noncompliant + WithValue(x) => x.hash(&mut state), +} +---- + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +enum Foo { Empty, WithValue(u8) } +use Foo::*; + +let mut state = DefaultHasher::new(); +let my_enum = Foo::Empty; + +match my_enum { + Empty => 0_u8.hash(&mut state), // Compliant + WithValue(x) => x.hash(&mut state), +} +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unit_hash