Compare commits
7 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
af10f1fd3d | ||
![]() |
5181655269 | ||
![]() |
c2c26fa255 | ||
![]() |
80a1cdd7ee | ||
![]() |
10634b11f7 | ||
![]() |
e0e4d95f54 | ||
![]() |
5cd33efb5d |
24
rules/S7465/java/metadata.json
Normal file
24
rules/S7465/java/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Unused arguments in lambda expressions should be denoted with an unnamed variable pattern \"_\"",
|
||||||
|
"type": "CODE_SMELL",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "2min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"java22"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Minor",
|
||||||
|
"ruleSpecification": "RSPEC-7465",
|
||||||
|
"sqKey": "S7465",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "targeted",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"MAINTAINABILITY": "LOW"
|
||||||
|
},
|
||||||
|
"attribute": "CLEAR"
|
||||||
|
}
|
||||||
|
}
|
39
rules/S7465/java/rule.adoc
Normal file
39
rules/S7465/java/rule.adoc
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
== Why is this an issue?
|
||||||
|
|
||||||
|
Library authors often design versatile APIs to handle diverse scenarios.
|
||||||
|
When such an API takes a function as an argument, we may find ourselves
|
||||||
|
writing a lambda expression where some arguments are unused.
|
||||||
|
Naming these arguments can lead to unnecessary verbosity.
|
||||||
|
Java 22 introduces the unnamed variable pattern `_` that clearly indicates
|
||||||
|
the intent not to use the variable.
|
||||||
|
|
||||||
|
== How to fix it
|
||||||
|
In order to clearly indicate the intent, unused arguments should be denoted
|
||||||
|
with an unnamed variable pattern `_`.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
[source,java,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
BiFunction<Integer,Integer,Integer> f = (a, unused) -> a; // Noncompliant
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
firstValueMap.merge(event.id, event.value, (oldValue, unused) -> oldValue); // Noncompliant
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
[source,java,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
BiFunction<Integer,Integer,Integer> f2 = (a, _) -> a;
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
firstValueMap.merge(event.id, event.value, (oldValue, _) -> oldValue);
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* OpenJDK - https://openjdk.org/jeps/456[JEP 456: Unnamed Variables & Patterns]
|
2
rules/S7465/metadata.json
Normal file
2
rules/S7465/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user