Skip to content

Commit 81b0261

Browse files
mahfouz72romani
authored andcommitted
Issue #15059: New Check UnusedLambdaParameterShouldBeUnnamed
1 parent c976754 commit 81b0261

File tree

34 files changed

+1312
-3
lines changed

34 files changed

+1312
-3
lines changed

config/checker-framework-suppressions/checker-nullness-optional-interning-suppressions.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,13 @@
25242524
<lineContent>if (parentToSkip != ast &amp;&amp; isExprSurrounded(ast)) {</lineContent>
25252525
</checkerFrameworkError>
25262526

2527+
<checkerFrameworkError unstable="false">
2528+
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.java</fileName>
2529+
<specifier>dereference.of.nullable</specifier>
2530+
<message>dereference of possibly-null reference lambdaParameters.peek()</message>
2531+
<lineContent>&amp;&amp; ast.equals(lambdaParameters.peek().enclosingLambda)) {</lineContent>
2532+
</checkerFrameworkError>
2533+
25272534
<checkerFrameworkError unstable="false">
25282535
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.java</fileName>
25292536
<specifier>argument</specifier>

config/checkstyle-checks.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@
590590
-->
591591
<property name="severity" value="ignore"/>
592592
</module>
593+
<module name="UnusedLambdaParameterShouldBeUnnamed">
594+
<!--
595+
Unnamed lambda parameters will not compile on java versions before 21. As
596+
we migrate to jdk 21 we will use this check.
597+
-->
598+
<property name="severity" value="ignore"/>
599+
</module>
593600
<module name="UnusedLocalVariable"/>
594601
<module name="VariableDeclarationUsageDistance"/>
595602
<module name="WhenShouldBeUsed">

config/checkstyle-non-main-files-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@
261261
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]unnecessarysemicoloninenumeration.xml.template"/>
262262
<suppress id="propertiesMacroMustExist"
263263
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]unusedcatchparametershouldbeunnamed.xml.template"/>
264+
<suppress id="propertiesMacroMustExist"
265+
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]unusedlambdaparametershouldbeunnamed.xml.template"/>
264266
<suppress id="propertiesMacroMustExist"
265267
files="src[\\/]xdocs[\\/]checks[\\/]coding[\\/]unusedlocalvariable.xml.template"/>
266268
<suppress id="propertiesMacroMustExist"

config/jsoref-spellchecker/whitelist.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ unregister
13771377
unsubscribe
13781378
unusedcatchparametershouldbeunnamed
13791379
unusedimports
1380+
unusedlambdaparametershouldbeunnamed
13801381
unusedlocalvariable
13811382
uppercased
13821383
upperell

config/linkcheck-suppressions.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@
315315
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.CatchParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)">#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
316316
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.CatchParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)">../../checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.CatchParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
317317
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
318+
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.html#%3Cinit%3E()">#%3Cinit%3E()</a>: doesn't exist.
319+
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.LambdaParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)">#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
320+
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.LambdaParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)">../../checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.LambdaParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
318321
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.TypeDeclDesc.html#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)">#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
319322
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.TypeDeclDesc.html#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)">../../checks/coding/UnusedLocalVariableCheck.TypeDeclDesc.html#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
320323
<a href="apidocs/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.VariableDesc.html#%3Cinit%3E(java.lang.String)">#%3Cinit%3E(java.lang.String)</a>: doesn't exist.
@@ -1028,6 +1031,8 @@
10281031
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnnecessarySemicolonInTryWithResourcesCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/UnnecessarySemicolonInTryWithResourcesCheck.html#%3Cinit%3E()</a>: doesn't exist.
10291032
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.CatchParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)">com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.CatchParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
10301033
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.html#%3Cinit%3E()</a>: doesn't exist.
1034+
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.html#%3Cinit%3E()">com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.html#%3Cinit%3E()</a>: doesn't exist.
1035+
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.LambdaParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)">com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.LambdaParameterDetails.html#%3Cinit%3E(com.puppycrawl.tools.checkstyle.api.DetailAST,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
10311036
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.TypeDeclDesc.html#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)">com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.TypeDeclDesc.html#%3Cinit%3E(java.lang.String,int,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.
10321037
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.VariableDesc.html#%3Cinit%3E(java.lang.String)">com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.VariableDesc.html#%3Cinit%3E(java.lang.String)</a>: doesn't exist.
10331038
<a href="com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.VariableDesc.html#%3Cinit%3E(java.lang.String,com.puppycrawl.tools.checkstyle.api.DetailAST)">com/puppycrawl/tools/checkstyle/checks/coding/UnusedLocalVariableCheck.VariableDesc.html#%3Cinit%3E(java.lang.String,com.puppycrawl.tools.checkstyle.api.DetailAST)</a>: doesn't exist.

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,7 @@
32883288
<param>com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck*</param>
32893289
<param>com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanReturnCheck*</param>
32903290
<param>com.puppycrawl.tools.checkstyle.checks.coding.UnusedCatchParameterShouldBeUnnamedCheck*</param>
3291+
<param>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck*</param>
32913292
</targetClasses>
32923293
<targetTests>
32933294
<param>com.puppycrawl.tools.checkstyle.checks.coding.ArrayTrailingCommaCheckTest</param>
@@ -3316,6 +3317,7 @@
33163317
<param>com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheckTest</param>
33173318
<param>com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanReturnCheckTest</param>
33183319
<param>com.puppycrawl.tools.checkstyle.checks.coding.UnusedCatchParameterShouldBeUnnamedCheckTest</param>
3320+
<param>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheckTest</param>
33193321
</targetTests>
33203322
<avoidCallsTo>
33213323
<avoidCallsTo>com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil</avoidCallsTo>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3+
// Copyright (C) 2001-2024 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
///////////////////////////////////////////////////////////////////////////////////////////////
19+
20+
package org.checkstyle.suppressionxpathfilter;
21+
22+
import java.io.File;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29+
import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck;
30+
31+
public class XpathRegressionUnusedLambdaParameterShouldBeUnnamedTest
32+
extends AbstractXpathTestSupport {
33+
34+
private final String checkName = UnusedLambdaParameterShouldBeUnnamedCheck.class
35+
.getSimpleName();
36+
37+
@Override
38+
protected String getCheckName() {
39+
return checkName;
40+
}
41+
42+
@Test
43+
public void testSimple() throws Exception {
44+
final File fileToProcess =
45+
new File(getNonCompilablePath(
46+
"InputXpathUnusedLambdaParameterShouldBeUnnamedSimple.java"));
47+
48+
final DefaultConfiguration moduleConfig =
49+
createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
50+
51+
final String[] expectedViolation = {
52+
"9:41: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
53+
UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
54+
"x"),
55+
};
56+
57+
final List<String> expectedXpathQueries = Arrays.asList(
58+
"/COMPILATION_UNIT/CLASS_DEF["
59+
+ "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
60+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT["
61+
+ "@text='f']]/ASSIGN/LAMBDA/PARAMETERS",
62+
"/COMPILATION_UNIT/CLASS_DEF["
63+
+ "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
64+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
65+
+ "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='x']]",
66+
"/COMPILATION_UNIT/CLASS_DEF["
67+
+ "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
68+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
69+
+ "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS"
70+
+ "/PARAMETER_DEF[./IDENT[@text='x']]/MODIFIERS",
71+
"/COMPILATION_UNIT/CLASS_DEF["
72+
+ "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
73+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
74+
+ "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/"
75+
+ "PARAMETER_DEF[./IDENT[@text='x']]/TYPE",
76+
"/COMPILATION_UNIT/CLASS_DEF["
77+
+ "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
78+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
79+
+ "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='x']"
80+
);
81+
82+
runVerifications(moduleConfig, fileToProcess, expectedViolation,
83+
expectedXpathQueries);
84+
}
85+
86+
@Test
87+
public void testNested() throws Exception {
88+
final File fileToProcess =
89+
new File(getNonCompilablePath(
90+
"InputXpathUnusedLambdaParameterShouldBeUnnamedNested.java"));
91+
92+
final DefaultConfiguration moduleConfig =
93+
createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
94+
95+
final String[] expectedViolation = {
96+
"10:45: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
97+
UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
98+
"y"),
99+
};
100+
101+
final List<String> expectedXpathQueries = Arrays.asList(
102+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
103+
+ "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
104+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
105+
+ "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
106+
+ "/ASSIGN/LAMBDA/PARAMETERS",
107+
"/COMPILATION_UNIT/CLASS_DEF"
108+
+ "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
109+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
110+
+ "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
111+
+ "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]",
112+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT"
113+
+ "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
114+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
115+
+ "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
116+
+ "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/MODIFIERS",
117+
"/COMPILATION_UNIT/CLASS_DEF"
118+
+ "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
119+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
120+
+ "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]/ASSIGN"
121+
+ "/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/TYPE",
122+
"/COMPILATION_UNIT/CLASS_DEF"
123+
+ "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
124+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
125+
+ "./IDENT[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF["
126+
+ "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='y']"
127+
);
128+
runVerifications(moduleConfig, fileToProcess, expectedViolation,
129+
expectedXpathQueries);
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//non-compiled with javac: Compilable with Java21
2+
package org.checkstyle.suppressionxpathfilter.unusedcatchparametershouldbeunnamed;
3+
4+
import java.util.function.Function;
5+
6+
public class InputXpathUnusedLambdaParameterShouldBeUnnamedNested {
7+
8+
void test() {
9+
Function<Integer, Integer> f1 = (x) -> {
10+
Function<Integer, Integer> f = (y) -> x; // warn
11+
return 2;
12+
};
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//non-compiled with javac: Compilable with Java21
2+
package org.checkstyle.suppressionxpathfilter.unusedcatchparametershouldbeunnamed;
3+
4+
import java.util.function.Function;
5+
6+
public class InputXpathUnusedLambdaParameterShouldBeUnnamedSimple {
7+
8+
void test() {
9+
Function<Integer, Integer> f = (x) -> 1; // warn
10+
}
11+
}

src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ private static void fillChecksFromCodingPackage() {
603603
BASE_PACKAGE + ".checks.coding.UnusedLocalVariableCheck");
604604
NAME_TO_FULL_MODULE_NAME.put("UnusedCatchParameterShouldBeUnnamedCheck",
605605
BASE_PACKAGE + ".checks.coding.UnusedCatchParameterShouldBeUnnamedCheck");
606+
NAME_TO_FULL_MODULE_NAME.put("UnusedLambdaParameterShouldBeUnnamedCheck",
607+
BASE_PACKAGE + ".checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck");
606608
}
607609

608610
/**

0 commit comments

Comments
 (0)