UnnecessaryTypeArgumentsWithRecordPattern

Since Checkstyle 13.9.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 {
  private Box<String> box = null;
  private Box<Box<String>> nested = null;
  private Pair<String, Integer> pair = null;

  public void test() {
    // violation below 'Unnecessary type arguments with record pattern.'
    if (box instanceof Box<String>(var s)) {}
    if (box instanceof Box(var s)) {}

    // violation below 'Unnecessary type arguments with record pattern.'
    if (pair instanceof Pair<String, Integer>(var a, var b)) {}
    if (pair instanceof Pair(var a, var b)) {}

    // 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))) {}
    if (nested instanceof Box(Box(var s))) {}

    switch (nested) {
      // violation below 'Unnecessary type arguments with record pattern.'
      case Box<Box<String>>(Box(var s)) -> {}
      default -> {}
    }
    switch (nested) {
      case Box(Box(var 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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.coding. UnnecessaryTypeArgumentsWithRecordPatternCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker