Skip to content

Commit

Permalink
Issue checkstyle#15014: Update LambdaParameterName format to allow un…
Browse files Browse the repository at this point in the history
…named lambda parameters
  • Loading branch information
mahfouz72 authored and rnveach committed Jun 29, 2024
1 parent b0961ea commit ff0e428
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testDefault() throws Exception {

final DefaultConfiguration moduleConfig =
createModuleConfig(LambdaParameterNameCheck.class);
final String defaultPattern = "^[a-z][a-zA-Z0-9]*$";
final String defaultPattern = "^([a-z][a-zA-Z0-9]*|_)$";

final String[] expectedViolation = {
"7:44: " + getCheckMessage(LambdaParameterNameCheck.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* <li>
* Property {@code format} - Sets the pattern to match valid identifiers.
* Type is {@code java.util.regex.Pattern}.
* Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
* Default value is {@code "^([a-z][a-zA-Z0-9]*|_)$"}.
* </li>
* </ul>
* <p>
Expand All @@ -54,7 +54,7 @@ public class LambdaParameterNameCheck extends AbstractNameCheck {

/** Creates new instance of {@code LambdaParameterNameCheck}. */
public LambdaParameterNameCheck() {
super("^[a-z][a-zA-Z0-9]*$");
super("^([a-z][a-zA-Z0-9]*|_)$");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Checks lambda parameter names.
&lt;/p&gt;</description>
<properties>
<property default-value="^[a-z][a-zA-Z0-9]*$"
<property default-value="^([a-z][a-zA-Z0-9]*|_)$"
name="format"
type="java.util.regex.Pattern">
<description>Sets the pattern to match valid identifiers.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testParametersInLambda() throws Exception {
@Test
public void testLambdaParameterNameSwitchExpression() throws Exception {

final String pattern = "^[a-z][a-zA-Z0-9]*$";
final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";

final String[] expected = {
"19:35: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
Expand All @@ -94,4 +94,20 @@ public void testLambdaParameterNameSwitchExpression() throws Exception {
expected);
}

@Test
public void testLambdaParameterNameUnnamed() throws Exception {

final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";

final String[] expected = {
"30:36: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
"34:36: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
"37:36: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD_", pattern),
};

verifyWithInlineConfigParser(
getNonCompilablePath("InputLambdaParameterNameUnnamed.java"),
expected);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
LambdaParameterName
format = (default)^[a-z][a-zA-Z0-9]*$
format = (default)^([a-z][a-zA-Z0-9]*|_)$
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
LambdaParameterName
format = (default)^([a-z][a-zA-Z0-9]*|_)$
*/

//non-compiled with javac: Compilable with Java21
package com.puppycrawl.tools.checkstyle.checks.naming.lambdaparametername;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class InputLambdaParameterNameUnnamed {

void method(Object o) {
List<Integer> numbers = Arrays.asList(1, 2, 3);
numbers = numbers.stream()
.map(_ -> 0)
.toList();
List<String> strings = Arrays.asList("a", "b", "c");

System.out.println(
strings.stream().collect(Collectors.toMap(String::toUpperCase,
_ -> "NODATA"))); // ok, unnamed lambda parameter

System.out.println(
strings.stream().collect(Collectors.toMap(String::toUpperCase,
__ -> "NODATA"))); // violation, 'Name '__' must match.*'

System.out.println(
strings.stream().collect(Collectors.toMap(String::toUpperCase,
_BAD -> "NODATA"))); // violation, 'Name '_BAD' must match.*'
System.out.println(
strings.stream().collect(Collectors.toMap(String::toUpperCase,
BAD_ -> "NODATA"))); // violation, 'Name 'BAD_' must match.*'

switch (o) {
case Integer __ -> {} // ok, this is pattern variable not a lambda parameter
case String _ -> {}
default -> {}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {
"17:11: " + getCheckMessage(MSG_INVALID_PATTERN, "S", "^[a-z][a-zA-Z0-9]*$"),
"17:11: " + getCheckMessage(MSG_INVALID_PATTERN, "S", "^([a-z][a-zA-Z0-9]*|_)$"),
};

verifyWithInlineConfigParser(getPath("Example1.java"), expected);
Expand Down
2 changes: 1 addition & 1 deletion src/xdocs/checks/naming/lambdaparametername.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td>format</td>
<td>Sets the pattern to match valid identifiers.</td>
<td><a href="../../property_types.html#Pattern">Pattern</a></td>
<td><code>&quot;^[a-z][a-zA-Z0-9]*$&quot;</code></td>
<td><code>&quot;^([a-z][a-zA-Z0-9]*|_)$&quot;</code></td>
<td>8.11</td>
</tr>
</table>
Expand Down

0 comments on commit ff0e428

Please sign in to comment.