Repository: checkstyle/checkstyle

Problem statement:
InnerAssignmentCheck failed for one line code in Java 14 switch expression
I have read check documentation: https://checkstyle.org/config_coding.html#InnerAssignment
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

```bash
/var/tmp $ javac TestInnerAssignmentCheck.java

/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="InnerAssignment"/>
  </module>
</module>

/var/tmp $ cat TestInnerAssignmentCheck.java
public class TestInnerAssignmentCheck {
    private boolean flag;

    void getOperation(final String operation) {
        switch (operation) {
            case "Y" -> flag = true;  // violation 
            case "N" -> {
                flag = false;
            }
            default -> throw new UnsupportedOperationException();
        }
    }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.11.0-all.jar -c config.xml TestInnerAssignmentCheck.java
Starting audit...
[ERROR] /Users/hael/Code/Learn/Tools/checkstyle/TestInnerAssignmentCheck.java:6:30:
     Inner assignments should be avoided. [InnerAssignment]
Audit done.
Checkstyle ends with 1 errors.
```

---

**Describe what you expect in detail.**

Checkstyle behaves differently between one line code and code block for Java 14's Switch Expression. In the given code above, checkstyle will complain about `case "Y" -> flag = true;` but not `case "N" -> {flag = false;}`. I'm afraid that it's because `flag = true;` is equivalent to `return flag = true;`? But the return value is not used here in the switch expression, we just want to do the value assignment. 

Expected: no violations on such code.




Hints:
@romani @nrmancuso there is an open PR for this but no activity since jan.idk if it is abandoned.so can i take this or should we wait 
@mahfouz72 you can pick it up, I closed the PR

Patch:
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
--- 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/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckExamplesTest.java
--- 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
--- /dev/null
+++ b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java
@@ -0,0 +1,47 @@
+/*xml
+<module name="Checker">
+  <module name="TreeWalker">
+    <module name="InnerAssignment"/>
+  </module>
+</module>
+*/
+
+//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


Test patch:
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
--- 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
--- /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
+        });
+    }
+}