Skip to content

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: Modifer Checks #15112

@mahfouz72

Description

@mahfouz72

child of #14942 :

InterfaceMemberImpliedModifierCheck

doc : https://checkstyle.org/checks/modifier/interfacememberimpliedmodifier.html

Checks for implicit modifiers on interface members and nested types.

interface members can't be unnamed so this check is unrelated.


ModifierOrderCheck

doc : https://checkstyle.org/checks/modifier/modifierorder.html

unnamed variables are not class fields so all of the modifiers list mentioned in the check doc can't be applied to unnamed variables. The only allowed modifier on an unnamed variable is final (https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.4).

VariableModifier:
(one of)
Annotation final


RedundantModifierCheck

doc : https://checkstyle.org/checks/modifier/redundantmodifier.html

JLS :

If a declaration does not include an identifier, but instead includes the keyword _ (underscore), then the entity cannot be referred to by name. The following kinds of entity may be declared using an underscore:

unnamed variables are effectively final which means that whether the variable has final modifier or not. the variable can't be reassigned. Is this case should be considered as redundant modifier?

PS D:\CS\test> cat config.xml                                                    
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <module name="RedundantModifier"/>
  </module>
</module>
PS D:\CS\test> cat src/Test.java                                                 
public class Test {
    void m() throws Exception {
        try (final var a = lock()) {  // violation , resource is always considered final
        }
        final int _ = 0;  // should this be violation ? (unnamed variables is always considered final)
    }
    AutoCloseable lock() {
        return null;
    }
}
PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml  src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:3:14: Redundant 'final' modifier. [RedundantModifier]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 

Metadata

Metadata

Assignees

Labels

No labels
No 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