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..e8c6a5f2153 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 @@ -109,4 +109,24 @@ public void testTokensNotNull() { .isNotNull(); } + @Test + public void testInnerAssignmentSwitchAndSwitchExpression() throws Exception { + final String[] expected = { + "28:23: " + getCheckMessage(MSG_KEY), + "38:25: " + getCheckMessage(MSG_KEY), + "40:25: " + getCheckMessage(MSG_KEY), + "41:26: " + getCheckMessage(MSG_KEY), + "49:25: " + getCheckMessage(MSG_KEY), + "51:31: " + getCheckMessage(MSG_KEY), + "52:26: " + getCheckMessage(MSG_KEY), + "59:42: " + getCheckMessage(MSG_KEY), + "61:34: " + getCheckMessage(MSG_KEY), + "94:25: " + getCheckMessage(MSG_KEY), + "96:26: " + getCheckMessage(MSG_KEY), + "98:27: " + 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..b3ae1c66850 --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java @@ -0,0 +1,101 @@ +/* +InnerAssignment + +*/ + +//non-compiled with javac: Compilable with Java14 +package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment; + +public class InputInnerAssignmentSwitchAndSwitchExpression { + + 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, y = 0; + switch (mode) { + case 2, 4, 6 -> { + x = 2; + } + case 1, 3, 5 -> { + x = y = 1; // violation + } + case 0, 7, 8 -> x = 1; + } + } + + public void test3(int mode) { + int x = 0; + x = switch (mode) { + case 2 -> { + yield x = 2; // violation + } + case 1 -> x = 1; // violation + default -> x = 0; // violation + }; + } + + public void test4(int mode) { + int x = 0; + x = switch (mode) { + case 2, 4, 6 -> { + yield x = 2; // violation + } + case 1, 3, 5 -> x = 1; // violation + default -> x = 0; // violation + }; + } + + public void test5(String operation) { + boolean innerFlag, flag; + switch (operation) { + case "Y" -> flag = innerFlag = true; // violation + case "N" -> { + flag = innerFlag = false; // violation + } + } + } + + public void test6(int mode) { + int x = 0; + switch (mode) { + case 2: { + x = 2; + } + case 1: + x = 1; + } + } + + public void test7(int mode) { + int x = 0; + switch (mode) { + case 0: + case 1: + case 2: { + x = 2; + } + case 4: + case 5: + x = 1; + } + } + + public void test8(int mode) { + int x = 4; + System.out.println(switch (x) { + case 1 -> x = 1; // violation + case 2 -> { + yield x = 2; // violation + } + default -> x = 3; // violation + }); + } +} diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java index 732c5588872..5067b9ece0a 100644 --- a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java +++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java @@ -45,4 +45,17 @@ public void testExample1() throws Exception { verifyWithInlineConfigParser(getPath("Example1.java"), expected); } + + @Test + public void testExample2() throws Exception { + final String[] expected = { + "18:19: " + getCheckMessage(MSG_KEY), + "20:17: " + getCheckMessage(MSG_KEY), + "22:20: " + getCheckMessage(MSG_KEY), + "39:15: " + getCheckMessage(MSG_KEY), + }; + + verifyWithInlineConfigParser( + getNonCompilablePath("Example2.java"), expected); + } } diff --git a/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java new file mode 100644 index 00000000000..b102040c611 --- /dev/null +++ b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java @@ -0,0 +1,47 @@ +/*xml + + + + + +*/ + +//non-compiled with javac: Compilable with Java14 +package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment; + + +// xdoc section -- start +public class Example2 { + public void test1(int mode) { + int x = 0; + x = switch (mode) { + case 1 -> x = 1; // violation + case 2 -> { + yield x = 2; // violation + } + default -> x = 0; // violation + }; + } + public void test2(int mode) { + int x = 0; + switch(mode) { + case 2 -> { + x = 2; + } + case 1 -> x = 1; + } + } + public void test3(int mode) { + int x = 0, y = 0; + switch(mode) { + case 1: + case 2: { + x = y = 2; // violation + } + case 4: + case 5: + x = 1; + } + } +} +// xdoc section -- end diff --git a/src/xdocs/checks/coding/innerassignment.xml b/src/xdocs/checks/coding/innerassignment.xml index f0c8f7e2c01..2e8343bcd71 100644 --- a/src/xdocs/checks/coding/innerassignment.xml +++ b/src/xdocs/checks/coding/innerassignment.xml @@ -57,7 +57,7 @@ while ((line = bufferedReader.readLine()) != null); // OK </module> </module> -

Example:

+

Example 1:

public class Example1 { void foo() throws IOException { @@ -97,6 +97,42 @@ public class Example1 { boolean val; return val = true; // violation } +} + +

Example 2:

+ +public class Example2 { + public void test1(int mode) { + int x = 0; + x = switch (mode) { + case 1 -> x = 1; // violation + case 2 -> { + yield x = 2; // violation + } + default -> x = 0; // violation + }; + } + public void test2(int mode) { + int x = 0; + switch(mode) { + case 2 -> { + x = 2; + } + case 1 -> x = 1; + } + } + public void test3(int mode) { + int x = 0, y = 0; + switch(mode) { + case 1: + case 2: { + x = y = 2; // violation + } + case 4: + case 5: + x = 1; + } + } } diff --git a/src/xdocs/checks/coding/innerassignment.xml.template b/src/xdocs/checks/coding/innerassignment.xml.template index 1fda0c68092..3c55d84e3e6 100644 --- a/src/xdocs/checks/coding/innerassignment.xml.template +++ b/src/xdocs/checks/coding/innerassignment.xml.template @@ -55,12 +55,18 @@ while ((line = bufferedReader.readLine()) != null); // OK value="resources/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example1.java"/> -

Example:

+

Example 1:

+

Example 2:

+ + + +