Skip to content

Commit

Permalink
Issue checkstyle#13086: fix InnerAssignmentCheck In SwitchRuleAndExpr…
Browse files Browse the repository at this point in the history
…ession
  • Loading branch information
mahfouz72 committed Apr 16, 2024
1 parent 8c27c6d commit 83700c0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 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 @@ -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 {

Expand Down Expand Up @@ -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);
}

}
@@ -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
}
}
}

}

0 comments on commit 83700c0

Please sign in to comment.