-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Description
This issue will be the main tracker of all the work related to Record Pattern. It aims to support the new record pattern syntax introduced in Java 21. 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.
AST view: https://github.com/checkstyle/checkstyle/wiki/Record-Pattern-Preview---AST-strucuture
New tokens: RECORD_PATTERN_DEF
, RECORD_PATTERN_COMPONENTS
Similar/Related tokens and constructs: PATTERN_VARIABLE_DEF
, PATTERN_DEF
, LITERAL_INSTANCEOF
, RECORD_COMPONENT_DEF
, RECORD_COMPONENTS
,...
Java Enhancement Proposals
reviewing JEP did not provide explicit guidance on possible checks that require updates to accommodate this new feature.
Similar and Related Tokens
mahfouz@dell-g15:~$ grep -rl "PATTERN_VARIABLE_DEF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ grep -rl "PATTERN_DEF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ grep -rl "LITERAL_INSTANCEOF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ grep -rl "RECORD_COMPONENT_DEF" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ grep -rl "RECORD_COMPONENTS" $checks | cut -d/ -f14- >> checks.txt
mahfouz@dell-g15:~$ cat checks.txt | sort -u
- coding/EqualsAvoidNullCheck.java Add Check Support for Java 21 Record Pattern : EqualsAvoidNull #14952
- coding/HiddenFieldCheck.java Add Check Support for Java 21 Record Pattern : HiddenField #14964
- coding/IllegalTypeCheck.java Add Check Support for Java 21 Record Pattern : IllegalType #14946
- coding/MissingSwitchDefaultCheck.java Add Support for Java 21 Record Pattern : MissingSwitchDefault #14891
- coding/UnnecessaryParenthesesCheck.java Add Check Support for Java 21 Record Pattern Syntax : UnnecessaryParenthesesCheck #15003
- naming/AbbreviationAsWordInNameCheck.java Add Check Support for Java 21 Record Pattern Syntax : AbbreviationAsWordInName #15004
- naming/IllegalIdentifierNameCheck.java Add Check Support for Java 21 Record Pattern Syntax : PatternVariableName #15005
- naming/PatternVariableNameCheck.java Add Check Support for Java 21 Record Pattern Syntax: IllegalIdentifierName #15006
- whitespace/GenericWhitespaceCheck.java Add Check Support for Java 21 Record Pattern Syntax: GenericWhitespace #15008
- whitespace/OperatorWrapCheck.java Add Check Support for Java 21 Record Pattern Syntax: OperatorWrap #15007
Frequently Impacted Checks
- IllegalTokenText: no tokens text with this feature. We won't update this check
- Indentation: we need to open separate issue for this feature to see if we need to update it Add Check Support for Java 21 Record Pattern Syntax: Indentation #15055
- IllegalToken: this check supports all tokens. It should be updated Add Check Support for Java 21 Record Pattern Syntax: IllegalToken #15294
- Whitespace :
- MethodParamPad:
RECORD_DEF
is supported in this check Add Check Support for Java 21 Record Pattern : MethodParamPad #14966 - ParenPad: the new syntax uses parentheses. Add Check Support for Java 21 Record Pattern : ParenPad #14948
- NoWhitespaceAfter : check that there is no whitespace between the record pattern and the next parenthesis
RecordPattern (int x)
same asLITERAL_SYNCHRONIZED
in this check
Add Check Support for Java 21 Record Pattern Syntax: NoWhitespaceAfter #15009
- MethodParamPad:
Other Static Analysis tool
- Intellij Inspection:
- (Condition is covered by further condition -> null-check is redundant with instanceof operator): this rule is not related to record patterns only. It should apply for any pattern matching with instanceof. We can consider a new check
redundantNullCheckWithPatternMatching
- record pattern can be used: this rule can't be applied due to a limitation. Checkstyle is not type-aware
- (Condition is covered by further condition -> null-check is redundant with instanceof operator): this rule is not related to record patterns only. It should apply for any pattern matching with instanceof. We can consider a new check
- PMD: I don't see any updates in rules just grammer and parser updates
Good Source of Best Practices
the most encourged best practice stated for record pattern is to use it instead of the old destruction syntax. We can't make a check to enforce this because we are not type aware
Associated JEP's Recommendations (JEP 440)
If a record pattern names a generic record class but gives no type arguments (i.e., the record pattern uses a raw type) then the type arguments are always inferred. Inference works with nested record patterns. In fact it is possible to drop the type arguments in the outer record pattern as well, leading to the concise code
As of Java 21, type arguments for record patterns can be inferred by the compiler when not explicitly provided. JEP recommend to drop type arguments in record pattern as this leads to a more concise code. This guides us to consider implementing a new check named UnnecessaryTypeArgumentsWithRecordPattern
JEP Code Example:
static void test2(Box<Box<String>> bbs) {
if (bbs instanceof Box<Box<String>>(Box(var s))) {
System.out.println("String " + s);
}
if (bbs instanceof Box(Box(var s))) {
System.out.println("String " + s); // Here the compiler will infer that the entire instanceof pattern is Box<Box<String>>(Box<String>(var s)
}
}
---
Discover Similar Checks
We have a check that violate the parameter assignment. Paremeters assignment is often considered poor programming practice as it can lead to confusion and unexpected behavior. The same would apply to pattern variables.
We should consider implementing a new check similar to ParameterAssignment that violates the assignment of pattern variables
Potential New Checks
The previously mentioned approaches' analysis has led to the proposal of the following new checks.
-
RedundantNullCheckWithPatternMatching
: Enforce removing redundant null checks when using instance of operator.
Add Check Support for Java 21 Record Pattern : New Check UnnecessaryNullCheckWithInstanceof #14945 -
PatternVariableAssignment
: Similar to the ParameterAssignment check, enforce best practices by disallowing assignments to pattern variables.
Add Check Support for Java 21 Record Pattern : New Check PatternVariableAssignment #14949 -
UnnecessaryTypeArgumentsWithRecordPattern
: Ensure type arguments are dropped in record patterns for conciseness. Given the fact that they are always inferred.
New check UnnecessaryTypeArgumentsWithRecordPattern #15223