Create rule S7422: Unit values should not be hashed (#4763)
* Create rule S7422 * Update RSPEC * Update snippets --------- 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
66bae183f4
commit
1379adbc47
2
rules/S7422/metadata.json
Normal file
2
rules/S7422/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7422/rust/metadata.json
Normal file
24
rules/S7422/rust/metadata.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
42
rules/S7422/rust/rule.adoc
Normal file
42
rules/S7422/rust/rule.adoc
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user