styleguide icon indicating copy to clipboard operation
styleguide copied to clipboard

[Java] (JDK24 in my case) use of underscore variable triggers checkstyle LambdaParameterName

Open patpatpat123 opened this issue 8 months ago • 4 comments

Hello team,

I am using JDK 24, and since a couple of last Java versions, we can use the underscore sign to substitute a variable that will not be used, what Java called an unnamed variable.

https://medium.com/@benweidig/looking-at-java-22-unnamed-variables-patterns-f9de0b1e2750

For instance:

final boolean someboolean = Optional.ofNullable(getMap()).map(_ -> getHeaders()) [...]

While this is perfectly fine and compiles, checkstyle will flag:

LambdaParameterName

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <outputDirectory>target/reports/checkstyle</outputDirectory>
                    <configLocation>google_checks.xml</configLocation>
                    <outputFileFormat>xml</outputFileFormat>
                </configuration>
            </plugin>

Could you please help take into account the new java underscore unnamed variable?

Thank you very much and good day.

patpatpat123 avatar Apr 07 '25 03:04 patpatpat123

Hey Google, same issue there. Could you help?

ryleighmikami avatar May 06 '25 02:05 ryleighmikami

Issue should be reported without any reference to checkstyle. @patpatpat123 , @ryleighmikami , please report it properly

romani avatar May 06 '25 03:05 romani

We are planning to update the style guide to allow this, the intent is not to discourage unnamed variables or patterns.

cushon avatar Sep 22 '25 18:09 cushon

We can certainly update the style guide to make this clearer, but I don't think anything in it right now precludes the use of unnamed variables or patterns. Technically, _ is not an identifier so it is not covered by the rules about identifier naming. We don't currently have anything at all to say about patterns, unnamed or otherwise.

eamonnmcmanus avatar Sep 24 '25 23:09 eamonnmcmanus