Create rule S1612: Closures should be replaced with function pointers (#4700)
* Add rust to rule S1612 * Update RSPEC * Remove tag --------- 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
45cc311cd2
commit
8d3cf1eee3
@ -1,30 +1,2 @@
|
||||
{
|
||||
"title": "Lambdas should be replaced with method references",
|
||||
"type": "CODE_SMELL",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "LOW"
|
||||
},
|
||||
"attribute": "CLEAR"
|
||||
},
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "2min"
|
||||
},
|
||||
"tags": [
|
||||
"java8"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [],
|
||||
"legacyKeys": []
|
||||
},
|
||||
"defaultSeverity": "Minor",
|
||||
"ruleSpecification": "RSPEC-1612",
|
||||
"sqKey": "S1612",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "covered"
|
||||
}
|
||||
|
@ -1,2 +1,30 @@
|
||||
{
|
||||
"title": "Lambdas should be replaced with method references",
|
||||
"type": "CODE_SMELL",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "LOW"
|
||||
},
|
||||
"attribute": "CLEAR"
|
||||
},
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "2min"
|
||||
},
|
||||
"tags": [
|
||||
"java8"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [],
|
||||
"legacyKeys": []
|
||||
},
|
||||
"defaultSeverity": "Minor",
|
||||
"ruleSpecification": "RSPEC-1612",
|
||||
"sqKey": "S1612",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "covered"
|
||||
}
|
||||
|
6
rules/S1612/rust/metadata.json
Normal file
6
rules/S1612/rust/metadata.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"title": "Closures should be replaced with function pointers",
|
||||
"tags": [
|
||||
"clippy"
|
||||
]
|
||||
}
|
28
rules/S1612/rust/rule.adoc
Normal file
28
rules/S1612/rust/rule.adoc
Normal file
@ -0,0 +1,28 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Using closures when a function pointer can be used instead can negatively impact readability and maintainability of the code. Closures can obscure the intent of the code, making it harder for other developers to understand what the code is doing at a glance. Function pointers, on the other hand, are more explicit and can make the code more concise and easier to read.
|
||||
|
||||
== How to fix it
|
||||
|
||||
Replace closures with function pointers when the closure simply calls a function. This makes the code more readable and easier to understand.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let result = Some('a').map(|s| s.to_uppercase());
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let result = Some('a').map(char::to_uppercase);
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
|
Loading…
x
Reference in New Issue
Block a user