Skip to content

Commit

Permalink
Issue checkstyle#15006: Add new input file for record patterns in ill…
Browse files Browse the repository at this point in the history
…egalIdentifierName
  • Loading branch information
mahfouz72 authored and nrmancuso committed Jul 7, 2024
1 parent 05fa3f0 commit d341662
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,21 @@ public void testIllegalIdentifierNameUnnamedVariable() throws Exception {
verifyWithInlineConfigParser(
getNonCompilablePath("InputIllegalIdentifierNameUnnamedVariables.java"), expected);
}

@Test
public void testIllegalIdentifierNameRecordPattern() throws Exception {
final String format = "(?i)^(?!(record|yield|var|permits|sealed)$).+$";

final String[] expected = {
"16:36: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
"17:47: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
"17:59: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
"17:74: " + getCheckMessage(MSG_INVALID_PATTERN, "sealed", format),
"26:28: " + getCheckMessage(MSG_INVALID_PATTERN, "permits", format),
"26:41: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
"30:39: " + getCheckMessage(MSG_INVALID_PATTERN, "permits", format),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputIllegalIdentifierNameRecordPattern.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
IllegalIdentifierName
format = (default)(?i)^(?!(record|yield|var|permits|sealed)$).+$
tokens = (default)CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, \
PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, ENUM_CONSTANT_DEF, PATTERN_VARIABLE_DEF, \
RECORD_DEF, RECORD_COMPONENT_DEF, LAMBDA
*/

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

public class InputIllegalIdentifierNameRecordPattern {

void m(Object o) {
if (o instanceof Point(int var, int _)) { } // violation, 'Name 'var' must match *.'
if (o instanceof ColorPoint(Point(int record, int yield), String sealed)) { }
// 3 violations above:
// 'Name 'record' must match *.'
// 'Name 'yield' must match *.'
// 'Name 'sealed' must match *.'
}

void m2(Object o) {
switch (o) {
case Point(int permits, int yield): {} break;
// 2 violations above:
// 'Name 'permits' must match *.'
// 'Name 'yield' must match *.'
case ColorPoint(Point(int permits, int _), String _): {}
// violation above, 'Name 'permits' must match *.'
default: {}
}
}

record Point(int x, int y) { }
record ColorPoint(Point p, String color) { }

}

0 comments on commit d341662

Please sign in to comment.