Skip to content

Issue #14688 : LocalVaiableNameCheck should allow unnamed variables b…#14978

Merged
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:localvariablename
Jun 14, 2024
Merged

Issue #14688 : LocalVaiableNameCheck should allow unnamed variables b…#14978
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:localvariablename

Conversation

@mahfouz72

@mahfouz72 mahfouz72 commented Jun 11, 2024

Copy link
Copy Markdown
Member

@mahfouz72

Copy link
Copy Markdown
Member Author

Github, generate report

@mahfouz72

mahfouz72 commented Jun 11, 2024

Copy link
Copy Markdown
Member Author

this check will break on legacy code that can use _ as a valid identifier (java 9 and below) there will be no violation on underscores.
if we don't want to do this we should introduce a new property

result = !isFinal && ScopeUtil.isLocalVariableDef(ast);
}
return result;
return result && !TokenUtil.isUnnamed(ast.findFirstToken(TokenTypes.IDENT));

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.

There are 2 ways to handle this issue. Either we update the existing property regexp to allow underscores, or we update the code to fully ignore single underscore variable names.

The regexp allows anyone to change it and what it accepts. Changing it would allow the users to backward compatible this change.

Change the code hard codes that this is to be a full on ignore which can't be undone. This is similar to ignore final variables, as another check handles this. If someone wanted to configure CS to violate underscore variables again, they would have to use another check, IllegalTokenText or possibly IllegalIdentifierName.

Are we sure we want to basically break compatibility and hard code the ignore? I was originally thinking we would just update the property. I am leaning more to the property just to remain compatible with those not on 21 yet. I remember somewhere we said we wanted to keep backwards compatibility with another check.

@nrmancuso Feel free to review.

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 was originally thinking we would just update the property.

Yes, please do this @mahfouz72

@github-actions

Copy link
Copy Markdown
Contributor

@mahfouz72 mahfouz72 force-pushed the localvariablename branch 3 times, most recently from e13a5fd to 5692fca Compare June 12, 2024 08:10
@mahfouz72 mahfouz72 force-pushed the localvariablename branch from 5692fca to d3e56f0 Compare June 12, 2024 08:24
@mahfouz72

Copy link
Copy Markdown
Member Author

Github, generate site

@mahfouz72

Copy link
Copy Markdown
Member Author

Github, generate report

@github-actions

Copy link
Copy Markdown
Contributor

@nrmancuso

Copy link
Copy Markdown
Contributor

Report is a bit hard to consume, but looks good when you click into projects with an asymmetrical diff and find the places where unnamed variables are present, you can see that there violations have been removed.

Example: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/d3e56f0_2024095635/reports/diff/checkstyle/xref/home/runner/work/checkstyle/checkstyle/.ci-temp/contribution/checkstyle-tester/repositories/checkstyle/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammar/java21/InputUnnamedVariableBasic.java.html#L6

@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.

Approved, but please make a comment in the issue with the old regexp to help people to find it and restore this check's previous behavior.

@nrmancuso nrmancuso assigned rnveach and unassigned nrmancuso Jun 12, 2024
@rnveach

rnveach commented Jun 13, 2024

Copy link
Copy Markdown
Contributor

Since we will probably have more regexp property updates, change the message of the check to a static message so we can see new/removed violations.
https://checkstyle.org/config.html#Custom_messages
Ex: <message key="name.invalidPattern" value="Hello World" />

@mahfouz72

Copy link
Copy Markdown
Member Author

Github, generate report

@github-actions

Copy link
Copy Markdown
Contributor

@rnveach rnveach merged commit 4bd481d into checkstyle:master Jun 14, 2024
@mahfouz72 mahfouz72 deleted the localvariablename branch July 4, 2024 00:33
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.

LocalVariableNameCheck incorrectly flags variables named _ (see JEP456)

3 participants