Create rule S7421: Closures of type Fn(...) -> Ord
should not return the unit type (#4762)
* Create rule S7421 * Update RSPEC * Change severity to Critical --------- 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
1379adbc47
commit
85d974977b
2
rules/S7421/metadata.json
Normal file
2
rules/S7421/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7421/rust/metadata.json
Normal file
24
rules/S7421/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "Closures of `type Fn(...) -> Ord` should not return the unit type",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Critical",
|
||||
"ruleSpecification": "RSPEC-7421",
|
||||
"sqKey": "S7421",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
26
rules/S7421/rust/rule.adoc
Normal file
26
rules/S7421/rust/rule.adoc
Normal file
@ -0,0 +1,26 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Returning the unit type from a closure expecting an `Ord` type is likely a mistake caused by an extra semi-colon. This is because the unit type implements `Ord` but doesn't serve the intended sorting purpose, making the code misleading and potentially incorrect.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let mut twins = vec![(1, 1), (2, 2)];
|
||||
twins.sort_by_key(|x| { x.1; }); // Noncompliant: Closure returns unit type due to unnecessary semi-colon.
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let mut twins = vec![(1, 1), (2, 2)];
|
||||
twins.sort_by_key(|x| x.1); // Compliant: Closure correctly returns an `Ord` type.
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unit_return_expecting_ord
|
Loading…
x
Reference in New Issue
Block a user