Compare commits

...

7 Commits

Author SHA1 Message Date
Tomasz Tylenda
af10f1fd3d Rewritten 2025-03-28 20:50:21 +01:00
tomasz-tylenda-sonarsource
5181655269
Use 'unused' in noncompliant
Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
2025-03-28 17:53:04 +01:00
tomasz-tylenda-sonarsource
c2c26fa255
JEP Link
Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
2025-03-28 17:52:27 +01:00
tomasz-tylenda-sonarsource
80a1cdd7ee
Update title
Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
2025-03-28 17:52:09 +01:00
Tomasz Tylenda
10634b11f7 Add description 2025-03-28 16:20:24 +01:00
Tomasz Tylenda
e0e4d95f54 Add metadata 2025-03-28 13:47:26 +01:00
tomasz-tylenda-sonarsource
5cd33efb5d Create rule S7465 2025-03-28 12:39:12 +00:00
3 changed files with 65 additions and 0 deletions

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

View 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]

View File

@ -0,0 +1,2 @@
{
}