-
-
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 #14506: False positive for UnusedLocalVariable
- Loading branch information
1 parent
40360c5
commit 6bb41a4
Showing
5 changed files
with
293 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
44 changes: 44 additions & 0 deletions
44
...heckstyle/checks/coding/unusedlocalvariable/InputUnusedLocalVariableSwitchExpression.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,44 @@ | ||
/* | ||
UnusedLocalVariable | ||
*/ | ||
|
||
//non-compiled with javac: Compilable with Java14 | ||
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable; | ||
|
||
public class InputUnusedLocalVariableSwitchExpression { | ||
|
||
public int complexSwitch(int input) { | ||
int check = 8; | ||
int something = 0; | ||
int line = 1; | ||
int line2 = 9; // violation 'Unused local variable 'line2'' | ||
return switch (check++) { | ||
case 0 -> { | ||
int x = 2; | ||
line2++; | ||
yield switch (x) { | ||
case 1 -> x++; | ||
case 2 -> x--; | ||
default -> 0; | ||
}; | ||
} | ||
case 1 -> { | ||
int y = input * 2; | ||
yield switch (++line) { | ||
case 2, 4 -> y / 2; | ||
case 6 -> y - 1; | ||
default -> -1; | ||
}; | ||
} | ||
case 2 -> { | ||
int checks = switch (something++) { | ||
case 1 -> input/2; | ||
default -> input; | ||
}; | ||
yield checks; | ||
} | ||
default -> 0; | ||
}; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...checkstyle/checks/coding/unusedlocalvariable/InputUnusedLocalVariableSwitchStatement.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,101 @@ | ||
/* | ||
UnusedLocalVariable | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable; | ||
|
||
public class InputUnusedLocalVariableSwitchStatement { | ||
public void testParameters0() { | ||
|
||
int i = 0; | ||
switch (i++) { | ||
default: | ||
case 0: | ||
System.out.println("one: "); | ||
break; | ||
case 1: | ||
System.out.println("two: " ); | ||
break; | ||
} | ||
|
||
} | ||
|
||
public void testParameters01() { | ||
int i = 0; | ||
switch (++i) { | ||
default: | ||
case 0: | ||
System.out.println("one: "); | ||
break; | ||
case 1: | ||
System.out.println("two: " ); | ||
break; | ||
} | ||
|
||
} | ||
|
||
public void testParameters() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
for (String test : tests) { | ||
switch (i++) { | ||
default: | ||
case 0: | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public void testParameters2() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
for (String test : tests) { | ||
switch (++i) { | ||
default: | ||
case 0: | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public void testParameters3() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
for (String test : tests) { | ||
switch (--i) { | ||
default: | ||
case 0: | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public void testParameters4() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
for (String test : tests) { | ||
switch (i--) { | ||
default: | ||
case 0: | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...heckstyle/checks/coding/unusedlocalvariable/InputUnusedLocalVariableSwitchStatement2.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,104 @@ | ||
/* | ||
UnusedLocalVariable | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable; | ||
|
||
public class InputUnusedLocalVariableSwitchStatement2 { | ||
public void testParameters0() { | ||
|
||
int i = 0; | ||
switch (i+1) { | ||
default: | ||
case 0: | ||
System.out.println("one: "); | ||
break; | ||
case 1: | ||
System.out.println("two: " ); | ||
break; | ||
} | ||
|
||
} | ||
|
||
public void testParameters01() { | ||
int i = 0; | ||
switch (1+i) { | ||
default: | ||
case 0: | ||
System.out.println("one: "); | ||
break; | ||
case 1: | ||
System.out.println("two: " ); | ||
break; | ||
} | ||
|
||
} | ||
|
||
public void testParameters() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
for (String test : tests) { | ||
switch (i+1) { | ||
default: | ||
case 0: | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public void testParameters2() { | ||
String[] tests = { "one", "two" }; | ||
int i = 0; | ||
i++; | ||
int j = 0; // violation 'Unused local variable 'j'' | ||
for (String test : tests) { | ||
switch (i) { | ||
default: | ||
case 0: | ||
j++; | ||
System.out.println("one: " + test); | ||
break; | ||
case 1: | ||
System.out.println("two: " + test); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public void testParameters4() { | ||
int i = 2; | ||
int j = 1; | ||
switch (j) { | ||
case 1: | ||
System.out.println("j is 1"); | ||
break; | ||
case 2: | ||
System.out.println("j is 2"); | ||
System.out.println("i is: " + i); | ||
break; | ||
default: | ||
System.out.println("j is something else"); | ||
} | ||
} | ||
|
||
public void testParameters5() { | ||
int i = 2; | ||
int j = 1; | ||
switch (++j) { | ||
case 1: | ||
System.out.println("j is 1"); | ||
break; | ||
case 2: | ||
System.out.println("j is 2"); | ||
System.out.println("i is: " + ++i); | ||
break; | ||
default: | ||
System.out.println("j is something else"); | ||
} | ||
} | ||
} |