UnnecessaryTypeArgumentsWithRecordPattern
Since Checkstyle 13.4.0
Description
Checks for unnecessary explicit type arguments in record patterns.
When a record pattern declares explicit type arguments that can be inferred by the compiler, those type arguments are redundant and reduce readability.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="UnnecessaryTypeArgumentsWithRecordPattern"/>
</module>
</module>
Examples of violations:
public class Example1 {
public void test() {
record Box<T>(T t) {}
record Pair<A, B>(A a, B b) {}
Box<String> box = null;
Box<Box<String>> nested = null;
Pair<String, Integer> pair = null;
// violation below, 'Unnecessary type arguments with record pattern.'
if (box instanceof Box<String>(var s)) {
System.out.println(s);
}
// 2 violations 3 lines below:
// 'Unnecessary type arguments with record pattern.'
// 'Unnecessary type arguments with record pattern.'
if (nested instanceof Box<Box<String>>(Box<String>(var s))) {
System.out.println(s);
}
// violation below, 'Unnecessary type arguments with record pattern.'
if (pair instanceof Pair<String, Integer>(var a, var b)) {
System.out.println(a + " " + b);
}
switch (nested) {
// violation below, 'Unnecessary type arguments with record pattern.'
case Box<Box<String>>(Box(var s)) ->
System.out.println(s);
default -> {}
}
if (box instanceof Box(var s)) { // ok
System.out.println(s);
}
if (box instanceof Box<?> (var s)) { // ok
System.out.println(s);
}
switch (nested) {
case Box(Box(var s)) -> // ok
System.out.println(s);
default -> {}
}
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding






