From 83700c081ff79874c17c04dd3a3fdbc69694d17d Mon Sep 17 00:00:00 2001 From: mahfouz72 Date: Wed, 17 Apr 2024 00:04:28 +0200 Subject: [PATCH] Issue #13086: fix InnerAssignmentCheck In SwitchRuleAndExpression --- .../checks/coding/InnerAssignmentCheck.java | 1 + .../coding/InnerAssignmentCheckTest.java | 15 ++++++ ...erAssignmentSwitchAndSwitchExpression.java | 51 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.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 5555b2fd766..3fc5ba33430 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, TokenTypes.LITERAL_SWITCH, TokenTypes.SLIST}, }; /** 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 310964406d5..506d14687b1 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 @@ -27,6 +27,8 @@ import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; import com.puppycrawl.tools.checkstyle.utils.CommonUtil; +import java.io.IOException; + public class InnerAssignmentCheckTest extends AbstractModuleTestSupport { @@ -109,4 +111,17 @@ public void testTokensNotNull() { .isNotNull(); } + @Test + public void testInnerAssignmentSwitchAndSwitchExpression() throws Exception { + final String[] expected = { + "22:25: " + getCheckMessage(MSG_KEY), + "24:25: " + getCheckMessage(MSG_KEY), + "41:42: " + getCheckMessage(MSG_KEY), + "43:34: " + getCheckMessage(MSG_KEY), + }; + verifyWithInlineConfigParser( + getNonCompilablePath("InputInnerAssignmentSwitchAndSwitchExpression.java"), + expected); + } + } diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java new file mode 100644 index 00000000000..7bd328e482c --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java @@ -0,0 +1,51 @@ +/* +InnerAssignment + + +*/ + + +//non-compiled with javac: Compilable with Java14 +public class InputInnerAssignmentSwitchRuleAndSwitchExpression { + + public void test1(int mode) { + int x = 0; + switch(mode) { + case 2 -> { + x = 2; + } + case 1 -> x = 1; + } + } + + public void test2(int mode) { + int x = 0; + x = switch (mode) { + case 2 -> { + yield x = 2; // violation + } + case 1 -> x = 1; // violation + default -> throw new UnsupportedOperationException(); + }; + } + public void test3(int mode) { + int x = 0; + switch(mode) { + case 2 : { + x = 2; + } + case 1 : x = 1; + } + } + + public void test4(String operation) { + boolean innerFlag; + switch (operation) { + case "Y" -> flag = innerFlag = true; // violation + case "N" -> { + flag = innerFlag = false; // violation + } + } + } + +}