Skip to content

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: FinalParameter  #14963

@mahfouz72

Description

@mahfouz72

child of #14942

I have read check documentation: https://checkstyle.org/checks/misc/finalparameters.html#FinalParameters
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

PS D:\CS\test> javac src/Test.java
PS D:\CS\test> cat src/Test.java
import java.util.PriorityQueue;
import java.util.Queue;

class Test {
    void test() {
        Queue<Integer> q = new PriorityQueue<>();
        q.add(1);
        for (Integer _ : q) {  // violation
            q.poll();
        }
        try {
            int x = 1 / 0;
        } catch (Exception _) {       // violation
            System.out.println("division by zero");
        }
    }
}

PS D:\CS\test> cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>
 <module name="TreeWalker">
    <module name="FinalParameters">
        <property name="tokens" value="FOR_EACH_CLAUSE, LITERAL_CATCH"/>
    </module>
 </module>
</module>
PS D:\CS\test> java  -jar checkstyle-10.14.2-all.jar -c config.xml  src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:8:14: Parameter _ should be final. [FinalParameters]
[ERROR] D:\CS\test\src\Test.java:13:18: Parameter _ should be final. [FinalParameters]
Audit done.
Checkstyle ends with 2 errors.



Describe what you expect in detail.

check doc :

Rationale: Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.

JLS:

If a declaration does not include an identifier, but instead includes the reserved keyword _ (underscore), then the entity cannot be referred to by name.

So I consider this as a false positive unnamed variables can't be referred to by name and Their value can't change so there is no point to require it to be final


Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions