Skip to content

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax  #14942

@mahfouz72

Description

@mahfouz72

This issue will be the main tracker of all the work related to Unnamed Variables & Patterns. It will involve updating existing checks to ensure they correctly handle this new syntax and analysis of possible static analysis coverage (new checks) for this new language feature.


new tokens: UNNAMED_PATTERN_DEF.
note: For unnamed variables we allow this token to be an identifier only that matches _.
similar/related tokens: VARIABLE_DEF, PATTERN_VARIABLE_DEF.

related constructs:

  1. formal parameter of a lambda expression: LambdaParameterNameCheck should be updated. The parameter name can be _ Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: LambdaParameterName #15014
  2. resource specification of a try-with-resources: we have no check that checks the naming of a resource
  3. exception parameter of a catch block: CatchParameterName should be updated. Also FinalParameters should be updated. If the parameter is unnamed then the check should request it to be final
    Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: CatchParameterName #15015
  4. header of an enhanced for loop: FinalParameters should be updated. If the parameter is unnamed then the check shouldn't request it to be final.
  5. The header of a basic for loop: variables in the header of for loop are just local variables so we should update LocalVariableNameCheck

Java Enhancement Proposals (JEP 456)

We expect developers of static analysis tools to understand the new role of underscore for unnamed variables and avoid flagging the non-use of such variables in modern code.

  1. we should update unusedLocalVariableCheck to ensure it does not incorrectly violate unnamed variables.

The try-with-resources statement is always used for its side effect, namely the automatic closing of resources. so the name of the resource variable is irrelevant. the following code acquires and automatically releases a context:

try (var acquiredContext = ScopedContext.acquire()) {
... acquiredContext not used ...
}
The name acquiredContext is just clutter, so it would be nice to elide it.

  1. we should update unusedLocalVariableCheck to violate the unused resources as JEP recommends. We should elide it ( declare it as an unnamed variable). The same would apply to exception parameters in catch blocks

Similar and Related Tokens

mahfouz@dell-g15:~$ grep -rl "VARIABLE_DEF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ grep -rl "PATTERN_VARIABLE_DEF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ cat checks.txt | sort -u

Frequently Impacted Checks


Other Static Analysis tool

  • Intellij Inspection:

    • unused declaration: Pattern variable is never used -> replace with unnamed pattern: we should implement a similar check that suggests that unused pattern variable should be unnamed pattern.
  • PMD: LocalVariableNamingConventions rule and FormalParameterNamingConventions rule are updated to allow unnamed variables by default in release 7.2.0 https://github.com/pmd/pmd/releases


Good Sources of Best Practices


Associated JEP's Recommendations (JEP 456)

All recommendations in the JEP emphasize that unused patterns should remain unnamed to reduce the visual cost and improve the readability of record patterns.


Discover Similar Checks

We have a check that violates the unused local variable. We should consider implementing a new check similar to it (or extending this check) to violate the non-use of pattern variables. It should be declared as an unnamed pattern or unnamed pattern variable.


UnusedLocalVariables Updates

from #14960

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions