Skip to content

Commit

Permalink
Issue #13086: fix InnerAssignmentCheck In SwitchRuleAndExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
mahfouz72 authored and rnveach committed May 12, 2024
1 parent 9349abe commit e7facb0
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 2 deletions.
Expand Up @@ -101,6 +101,7 @@ public class InnerAssignmentCheck
TokenTypes.RESOURCE_SPECIFICATION,
},
{TokenTypes.EXPR, TokenTypes.LAMBDA},
{TokenTypes.EXPR, TokenTypes.SWITCH_RULE, TokenTypes.LITERAL_SWITCH, TokenTypes.SLIST},
};

/**
Expand Down
Expand Up @@ -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);
}
}
@@ -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
});
}
}
Expand Up @@ -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);
}
}
@@ -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
38 changes: 37 additions & 1 deletion src/xdocs/checks/coding/innerassignment.xml
Expand Up @@ -57,7 +57,7 @@ while ((line = bufferedReader.readLine()) != null); // OK
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example1-code">Example:</p>
<p id="Example1-code">Example 1:</p>
<source>
public class Example1 {
void foo() throws IOException {
Expand Down Expand Up @@ -97,6 +97,42 @@ public class Example1 {
boolean val;
return val = true; // violation
}
}
</source>
<p id="Example2-code">Example 2:</p>
<source>
public class Example2 {
public void test1(int mode) {
int x = 0;
x = switch (mode) {
case 1 -&gt; x = 1; // violation
case 2 -&gt; {
yield x = 2; // violation
}
default -&gt; x = 0; // violation
};
}
public void test2(int mode) {
int x = 0;
switch(mode) {
case 2 -&gt; {
x = 2;
}
case 1 -&gt; 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;
}
}
}
</source>
</subsection>
Expand Down
8 changes: 7 additions & 1 deletion src/xdocs/checks/coding/innerassignment.xml.template
Expand Up @@ -55,12 +55,18 @@ while ((line = bufferedReader.readLine()) != null); // OK
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">Example:</p>
<p id="Example1-code">Example 1:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example1.java"/>
<param name="type" value="code"/>
</macro>
<p id="Example2-code">Example 2:</p>
<macro name="example">
<param name="path"
value="resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java"/>
<param name="type" value="code"/>
</macro>
</subsection>

<subsection name="Example of Usage" id="Example_of_Usage">
Expand Down

0 comments on commit e7facb0

Please sign in to comment.