Skip to content

Commit f5bcfd8

Browse files
vivek-0509romani
authored andcommitted
Issue #18283: allow unnamed variables (_) in naming patterns
1 parent 25322e2 commit f5bcfd8

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

src/it/java/com/google/checkstyle/test/chapter5naming/rule527localvariablenames/LocalVariableNamesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ public void testOneChar() throws Exception {
5252
verifyWithWholeConfig(getPath("InputLocalVariableNameOneCharVarName.java"));
5353
}
5454

55+
@Test
56+
public void testUnnamedVariables() throws Exception {
57+
verifyWithWholeConfig(getPath("InputUnnamedVariables.java"));
58+
}
59+
60+
@Test
61+
public void testPatternVariableNameUnnamed() throws Exception {
62+
verifyWithWholeConfig(getPath("InputPatternVariableNameUnnamed.java"));
63+
}
64+
5565
}

src/it/resources/com/google/checkstyle/test/chapter5naming/rule526parameternames/InputCatchParameterName.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,26 @@ public class InputCatchParameterName {
9494
/* foo */
9595
} catch (Exception Ex) { // violation 'Catch parameter name 'Ex' must match pattern'
9696
}
97+
98+
try {
99+
/* foo */
100+
} catch (Exception _) {
101+
// handle
102+
}
103+
104+
try {
105+
/* foo */
106+
} catch (Exception _ex) { // violation 'Catch parameter name '_ex' must match pattern'
107+
}
108+
109+
try {
110+
/* foo */
111+
} catch (Exception e_x) { // violation 'Catch parameter name 'e_x' must match pattern'
112+
}
113+
114+
try {
115+
/* foo */
116+
} catch (Exception ex_) { // violation 'Catch parameter name 'ex_' must match pattern'
117+
}
97118
}
98119
}

src/it/resources/com/google/checkstyle/test/chapter5naming/rule526parameternames/InputLambdaParameterName.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ public class InputLambdaParameterName {
2828

2929
BiFunction<String, String, Integer> goodNamedParameters =
3030
(first, second) -> (first + second).length();
31+
32+
BiFunction<String, String, String> unnamedParam = (first, _) -> first;
33+
34+
BiFunction<String, String, String> underscoreStart =
35+
(first, _second) -> first; // violation 'Lambda parameter name '_second' must match pattern'
36+
37+
BiFunction<String, String, String> underscoreMiddle =
38+
(first, sec_ond) -> first; // violation 'Lambda parameter name 'sec_ond' must match pattern'
39+
40+
BiFunction<String, String, String> underscoreEnd =
41+
(first, second_) -> first; // violation 'Lambda parameter name 'second_' must match pattern'
3142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Java22
2+
3+
package com.google.checkstyle.test.chapter5naming.rule527localvariablenames;
4+
5+
/** Tests unnamed pattern variables. */
6+
public class InputPatternVariableNameUnnamed {
7+
8+
void test(Object o1) {
9+
if (o1 instanceof String _) {
10+
System.out.println("ok");
11+
}
12+
13+
// violation below 'Pattern variable name '_s' must match pattern'
14+
if (o1 instanceof String _s) {
15+
System.out.println(_s);
16+
}
17+
18+
// violation below 'Pattern variable name 's_t' must match pattern'
19+
if (o1 instanceof String s_t) {
20+
System.out.println(s_t);
21+
}
22+
23+
// violation below 'Pattern variable name 'st_' must match pattern'
24+
if (o1 instanceof String st_) {
25+
System.out.println(st_);
26+
}
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Java22
2+
3+
package com.google.checkstyle.test.chapter5naming.rule527localvariablenames;
4+
5+
import java.util.HashMap;
6+
7+
/** Tests that unnamed variables (_) are allowed per Google Style. */
8+
public class InputUnnamedVariables {
9+
10+
record Point(int x, int y) {}
11+
12+
void testUnnamedVariables() {
13+
for (var _ : new String[] {"foo"}) {
14+
System.out.println("iteration");
15+
}
16+
17+
var _ = new Thread();
18+
19+
switch ("foo") {
20+
case String _ -> System.out.println("bar");
21+
}
22+
23+
switch (new Point(0, 0)) {
24+
case Point(int _, _) -> System.out.println("point");
25+
}
26+
27+
try {
28+
// something
29+
} catch (Error _) {
30+
// error handling
31+
}
32+
33+
var map = new HashMap<String, Integer>();
34+
map.computeIfAbsent("foo", _ -> 23);
35+
36+
var _name = "test"; // violation 'Local variable name '_name' must match pattern'
37+
var na_me = "test"; // violation 'Local variable name 'na_me' must match pattern'
38+
var name_ = "test"; // violation 'Local variable name 'name_' must match pattern'
39+
}
40+
}

src/main/resources/google_checks.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,22 @@
265265
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
266266
</module>
267267
<module name="LambdaParameterName">
268-
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
268+
<property name="format" value="^(_|[a-z]([a-z0-9][a-zA-Z0-9]*)?)$"/>
269269
<message key="name.invalidPattern"
270270
value="Lambda parameter name ''{0}'' must match pattern ''{1}''."/>
271271
</module>
272272
<module name="CatchParameterName">
273-
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
273+
<property name="format" value="^(_|[a-z]([a-z0-9][a-zA-Z0-9]*)?)$"/>
274274
<message key="name.invalidPattern"
275275
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
276276
</module>
277277
<module name="LocalVariableName">
278-
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
278+
<property name="format" value="^(_|[a-z]([a-z0-9][a-zA-Z0-9]*)?)$"/>
279279
<message key="name.invalidPattern"
280280
value="Local variable name ''{0}'' must match pattern ''{1}''."/>
281281
</module>
282282
<module name="PatternVariableName">
283-
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
283+
<property name="format" value="^(_|[a-z]([a-z0-9][a-zA-Z0-9]*)?)$"/>
284284
<message key="name.invalidPattern"
285285
value="Pattern variable name ''{0}'' must match pattern ''{1}''."/>
286286
</module>

0 commit comments

Comments
 (0)