-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #13086: fix InnerAssignmentCheck In SwitchRuleAndExpression
- Loading branch information
Showing
7 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...eckstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
InnerAssignment | ||
*/ | ||
package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment; | ||
|
||
//non-compiled with javac: Compilable with Java14 | ||
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; | ||
switch(mode) { | ||
case 2, 4, 6 -> { | ||
x = 2; | ||
} | ||
case 1, 3, 5 -> 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 | ||
}; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*xml | ||
<module name="Checker"> | ||
<module name="TreeWalker"> | ||
<module name="InnerAssignment"/> | ||
</module> | ||
</module> | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.innerassignment; | ||
|
||
//non-compiled with javac: Compilable with Java14 | ||
|
||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters