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:
github-actions[bot] 2025-03-19 13:38:32 +00:00 committed by GitHub
parent 45cc311cd2
commit 8d3cf1eee3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 28 deletions

View File

@ -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"
}

View File

@ -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"
}

View File

@ -0,0 +1,6 @@
{
"title": "Closures should be replaced with function pointers",
"tags": [
"clippy"
]
}

View 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