From acef6757fe07d3b70a669120b42bf75fb9bd9670 Mon Sep 17 00:00:00 2001 From: Aayush Date: Sat, 7 Oct 2023 22:51:44 +0530 Subject: [PATCH] Issue #13086: InnerAssignmentCheck failed for one line code --- .../checks/coding/InnerAssignmentCheck.java | 1 + .../coding/InnerAssignmentCheckTest.java | 8 +++++++ ...nputInnerAssignmentWithEnhancedSwitch.java | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentWithEnhancedSwitch.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java index 2a923a04e14..25426da59bb 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java @@ -101,6 +101,7 @@ public class InnerAssignmentCheck TokenTypes.RESOURCE_SPECIFICATION, }, {TokenTypes.EXPR, TokenTypes.LAMBDA}, + {TokenTypes.EXPR, TokenTypes.SWITCH_RULE}, }; /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckTest.java index f33cc2527ef..a9f6f1ca963 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckTest.java @@ -79,6 +79,14 @@ public void testInnerAssignmentNotInLoopContext() throws Exception { expected); } + @Test + public void testInnerAssignmentWithEnhancedSwitch() throws Exception { + final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; + verifyWithInlineConfigParser( + getNonCompilablePath("InputInnerAssignmentWithEnhancedSwitch.java"), + expected); + } + @Test public void testTokensNotNull() { final InnerAssignmentCheck check = new InnerAssignmentCheck(); diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentWithEnhancedSwitch.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentWithEnhancedSwitch.java new file mode 100644 index 00000000000..c459f20aa66 --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentWithEnhancedSwitch.java @@ -0,0 +1,21 @@ +/* +InnerAssignment + + +*/ +package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment; + +//non-compiled with javac: Compilable with Java14 +public class InputInnerAssignmentWithEnhancedSwitch { + + void method(final String operation) { + boolean flag = false; + switch (operation) { + case "Y" -> flag = true; + case "N" -> { + flag = false; + } + default -> throw new UnsupportedOperationException(); + } + } +}