From d341662927bfb463795d50c5aeb64149f05edbd5 Mon Sep 17 00:00:00 2001 From: mahfouz72 Date: Thu, 4 Jul 2024 03:29:35 +0300 Subject: [PATCH] Issue #15006: Add new input file for record patterns in illegalIdentifierName --- .../IllegalIdentifierNameCheckTest.java | 17 ++++++++ ...putIllegalIdentifierNameRecordPattern.java | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/illegalidentifiername/InputIllegalIdentifierNameRecordPattern.java diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/IllegalIdentifierNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/IllegalIdentifierNameCheckTest.java index c1d5813f3a9..4cc7de142f0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/IllegalIdentifierNameCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/IllegalIdentifierNameCheckTest.java @@ -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); + } } diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/illegalidentifiername/InputIllegalIdentifierNameRecordPattern.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/illegalidentifiername/InputIllegalIdentifierNameRecordPattern.java new file mode 100644 index 00000000000..b36cd339f24 --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/illegalidentifiername/InputIllegalIdentifierNameRecordPattern.java @@ -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) { } + +}