Skip to content

Issue #14950: Add new property to UnusedLocalVariable to suppress violtions on unnamed variables#15187

Merged
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:unused-local-variable
Jul 10, 2024
Merged

Issue #14950: Add new property to UnusedLocalVariable to suppress violtions on unnamed variables#15187
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:unused-local-variable

Conversation

@mahfouz72 mahfouz72 marked this pull request as draft July 2, 2024 17:59
@romani

romani commented Jul 2, 2024

Copy link
Copy Markdown
Member

If you move first commit to separate PR, I will merge it by single review.

@mahfouz72

Copy link
Copy Markdown
Member Author

@romani done #15189 please merge ASAP. because this PR will be blocked until the supplemental gets merged
I will drop the supplemental comment here and rebase after merging the other one.

@mahfouz72 mahfouz72 force-pushed the unused-local-variable branch from d1fc9ce to da3effb Compare July 3, 2024 01:34
@mahfouz72 mahfouz72 marked this pull request as ready for review July 3, 2024 01:45
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate site

@mahfouz72 mahfouz72 force-pushed the unused-local-variable branch from da3effb to 48f0cc6 Compare July 3, 2024 02:25
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

github-actions Bot commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

github-actions Bot commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

github-actions Bot commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

github-actions Bot commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72

Copy link
Copy Markdown
Member Author

@nrmancuso @rnveach ping

@rnveach rnveach self-assigned this Jul 5, 2024
@rnveach rnveach requested review from nrmancuso and rnveach July 5, 2024 16:14
@rnveach

rnveach commented Jul 5, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72 In the future, can you label the regressions for us on what they are if there are multiple and hide the ones we shouldn't be looking at if they are old. I believe GH regression action accepts labels as a parameter but I don't use it so I don't know what it is.

I expect to see regression on

@rnveach

rnveach commented Jul 5, 2024

Copy link
Copy Markdown
Contributor

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/48f0cc6_2024161154/reports/diff/checkstyle/index.html#A28
Am I missing something, why isn't there a removal on line 7? Is our check basically confused because there are 2 variables with the same name?
We ignore try/catch for lines 8-12, correct?

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/48f0cc6_2024161154/reports/diff/openjdk21/index.html#A1
I am reading this right and this file isn't compilable and so not an issue with this check?

@mahfouz72

Copy link
Copy Markdown
Member Author

Am I missing something, why isn't there a removal on line 7? Is our check basically confused because there are 2 variables with the same name?

There was no violation before check got confused on line 8. and registered _ as used because it is encountered in resource specification in try

PS D:\CS\test> cat src/Test.java                                                                                               
public class Test {
    void m() {
        int _ = 0; // ok
        try (final Lock _ = null) { } // check recognized '_' in resource as usage of the above variable
    }
    void m2() {
        int _ = 0; // violation
    }
    class Lock implements AutoCloseable {
        @Override
        public void close() {}
    }
}
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:7:9: Unused local variable '_'. [UnusedLocalVariable]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 


We ignore try/catch for lines 8-12, correct?

Yes, the check currently ignores try we should add support for try in this check in another issue and the exception parameter is not even considered a local variable so it is ik to get ignored by this check, catch parameters needs a new check


I am reading this right and this file isn't compilable and so not an issue with this check?

Yes

@mahfouz72 mahfouz72 force-pushed the unused-local-variable branch from 48f0cc6 to 749efc5 Compare July 6, 2024 23:58
@mahfouz72 mahfouz72 requested a review from rnveach July 7, 2024 00:06
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate site

@mahfouz72 mahfouz72 force-pushed the unused-local-variable branch 2 times, most recently from b012841 to 97f1f24 Compare July 8, 2024 12:58
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate site

Comment thread src/xdocs/checks/coding/unusedlocalvariable.xml.template Outdated

@nrmancuso nrmancuso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnveach

rnveach commented Jul 9, 2024

Copy link
Copy Markdown
Contributor

will be a bug for people who have used _ as a normal identifier before (java 9)

Please make an issue for this. We won't attach it to GSoC.

I am not sure if I understand this but yes it is normal to compile we can have more than one unnamed variable in a file (after Java 21), before java 9 this wouldn't compile because it was a valid identifier

Think this answers me. It won't compile pre-9 because of multiple variables with the same name, but after 21 it is because of the unnamed. I was wrongly thinking this was a pre-9 file.

@rnveach rnveach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good, but the following need to be done:

#15187 (comment)
#15187 (comment)

@rnveach rnveach assigned nrmancuso and unassigned rnveach Jul 9, 2024
@mahfouz72 mahfouz72 force-pushed the unused-local-variable branch from 97f1f24 to 2d34f38 Compare July 9, 2024 20:15
@mahfouz72

Copy link
Copy Markdown
Member Author

if they declare some local variable named _ and a resource _ in a try then the resource will cancel the violation of the local variable

@rnveach sorry I missed something. this won't compile before Java 9. You can't have two variables with the same name. accordingly, This is not a bug that affects either Java 9 or Java21+. Do you agree?

@nrmancuso nrmancuso assigned rnveach and unassigned nrmancuso Jul 10, 2024
@nrmancuso

Copy link
Copy Markdown
Contributor

I am good, but the following need to be done:

#15187 (comment) #15187 (comment)

Based on this comment, I will let @rnveach merge this PR once he is good.

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72 Isn't this a bug before Java 9 that there aren't 2 violations, both on the _ variables and not the resource?

$ cat TestClass.java
public class TestClass {
    void method1() {
int _ = 0;
    }

    void method2() {
int _ = 0;

try (Lock _ = null) {}
    }
}

$ cat TestConfig.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">
    <property name="charset" value="UTF-8"/>
    <property name="severity" value="warning"/>
    <property name="haltOnException" value="false"/>

    <module name="TreeWalker">
<module name="UnusedLocalVariable"/>
    </module>
</module>

$ java -jar checkstyle-10.17.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[WARN] TestClass.java:3:1: Unused local variable '_'. [UnusedLocalVariable]
Audit done.

@mahfouz72

Copy link
Copy Markdown
Member Author

void method2() {
int _ = 0;

try (Lock _ = null) {}
}

The thing is that this method is not compilable before java 9. You can't have 2 variables with the same name in a single scope

PS D:\CS\test> cat src/Test.java
public class Test {
    void test1() {
        int a = 1;
        try (var a = lock()) {

        }
    }
    AutoCloseable lock() {
        return null;
    }
}
PS D:\CS\test> javac src/Test.java
src\Test.java:4: error: variable a is already defined in method test1()
        try (var a = lock()) {
                 ^
1 error 

resource

validating a resource is another issue #15024

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

ok, maybe I will look into a bit but it seems a concern that this check thinks a try-with-resource is a usage. My concerns are resolved for now.

@mahfouz72

Copy link
Copy Markdown
Member Author

maybe I will look into a bit but it seems a concern that this check thinks a try-with-resource is a usage

It is a concern of course but doesn't result in a bug. if it works don't touch it : )

My concerns are resolved for now.

Then please merge when you can this is left for you to merge

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

if it works don't touch it

You can't prove it "works" if we can't say it will never be a problem. The only way we have seen it is with duplicate variable name which isn't valid. We haven't ruled out all circumstances, which is what I want to try and look into.

BTW, isn't this still a bug on Java 21+ if you don't suppress unnamed variables? Nothing says this has to be enabled if you are on a high enough java version.

@mahfouz72

mahfouz72 commented Jul 10, 2024

Copy link
Copy Markdown
Member Author

isn't this still a bug on Java 21+ if you don't suppress unnamed variables?

yes , but it is very nonsense if I am on java 21+ and want to violate all unnamed variables as unused

@mahfouz72

Copy link
Copy Markdown
Member Author

If you want I can opem issue to remember this once I am on laptop

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

Continuing on for bug at #15187 (comment),

It appears our IDENT handling might be too aggressive. If it finds an ident which doesn't match a few exclusions we register it as a possible variable usage.

It picked up Lock _ because _ is an ident and it looks to see if it is a variable and if so, it is being "used".

I am going to run regression with a change and see if anything pops up.

Edit:
Regression provided at https://rveach.no-ip.org/checkstyle/regression/1/

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

https://rveach.no-ip.org/checkstyle/regression/1/openjdk17/index.html#A3
It looks like we can't simply ignore resources altogether. This is a usage.

Making a change and trying another run.

@mahfouz72

Copy link
Copy Markdown
Member Author

It looks like we can't simply ignore resources altogether.

yeah, then it should be ignored onlt if it is a new variable declaration not a usage of previous variable

@rnveach

rnveach commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

https://rveach.no-ip.org/checkstyle/regression/2/
master...rnveach:checkstyle:pr_15187_test

Second regression showed less cases.

This still seems like a concern that ident is being so far reaching. I am not sure if we could satisfy pitest to merge a PR even if we did make an issue.

I am going to merge this PR for now.

@rnveach rnveach merged commit 15b5b73 into checkstyle:master Jul 10, 2024
@mahfouz72 mahfouz72 deleted the unused-local-variable branch May 9, 2025 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: UnusedLocalVariable

4 participants