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.
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
All recommendations in the JEP emphasize that unused patterns should remain unnamed to reduce the visual cost and improve the readability of record patterns.
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.
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:
LambdaParameterNameCheckshould be updated. The parameter name can be_Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: LambdaParameterName #15014CatchParameterNameshould be updated. AlsoFinalParametersshould be updated. If the parameter is unnamed then the check should request it to be finalAdd Check Support for Java 21 Unnamed Variables & Patterns Syntax: CatchParameterName #15015
FinalParametersshould be updated. If the parameter is unnamed then the check shouldn't request it to be final.LocalVariableNameCheckJava Enhancement Proposals (JEP 456)
unusedLocalVariableCheckto ensure it does not incorrectly violate unnamed variables.unusedLocalVariableCheckto 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 blocksSimilar and Related Tokens
_#15012_(see JEP456) #14688Frequently Impacted Checks
a. NoWhitespaceAfter: we should violate space after unnamed patterns
R(int x, _ , int y). Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: NoWhitespaceAfter #15224b. WhitespaceAround may be considered too. Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: WhitespaceAround #15225 (comment)
c. WhitespaceAfter: Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: WhitespaceAfter #15057
Other Static Analysis tool
Intellij Inspection:
PMD:
LocalVariableNamingConventionsrule andFormalParameterNamingConventionsrule are updated to allow unnamed variables by default in release 7.2.0 https://github.com/pmd/pmd/releasesGood 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.
Extend UnusedLocalVariable to support pattern variables #15085
UnusedLocalVariables Updates
from #14960
allowUnnamedVariablesto suppress violation on unnamed variables this will include local variables and variables in enhanced/basic for loop Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: UnusedLocalVariable #14950UnusedCatchParameterShouldBeUnnamedAdd Check Support for Java 21 Unnamed Variables & Patterns Syntax: New Check UnusedCatchParameterShouldBeUnnamed #15058UnusedLambdaParameterShouldBeUnnamedAdd Check Support for Java 21 Unnamed Variables & Patterns Syntax: New Check UnusedLambdaParameterShouldBeUnnamed #15059UnusedTryResourceShouldBeUnnamedNew Check UnusedTryResourceShouldBeUnnamed #15484