-
-
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 #13167: Fix NPE in UnusedLocalVariable check
- Loading branch information
1 parent
8605dd8
commit c15242f
Showing
6 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
config/pitest-suppressions/pitest-coding-2-suppressions.xml
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<suppressedMutations> | ||
<!-- until https://github.com/checkstyle/checkstyle/issues/14894 --> | ||
<mutation unstable="false"> | ||
<sourceFile>UnusedLocalVariableCheck.java</sourceFile> | ||
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck</mutatedClass> | ||
<mutatedMethod>getBlockContainingLocalAnonInnerClass</mutatedMethod> | ||
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator</mutator> | ||
<description>negated conditional</description> | ||
<lineContent>if (currentAst.getType() == TokenTypes.LAMBDA) {</lineContent> | ||
</mutation> | ||
|
||
<mutation unstable="false"> | ||
<sourceFile>UnusedLocalVariableCheck.java</sourceFile> | ||
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck</mutatedClass> | ||
<mutatedMethod>getBlockContainingLocalAnonInnerClass</mutatedMethod> | ||
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator> | ||
<description>removed conditional - replaced equality check with true</description> | ||
<lineContent>if (currentAst.getType() == TokenTypes.LAMBDA) {</lineContent> | ||
</mutation> | ||
|
||
<mutation unstable="false"> | ||
<sourceFile>UnusedLocalVariableCheck.java</sourceFile> | ||
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck</mutatedClass> | ||
<mutatedMethod>isInsideLocalAnonInnerClass</mutatedMethod> | ||
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator</mutator> | ||
<description>negated conditional</description> | ||
<lineContent>if (currentAst.getType() == TokenTypes.SLIST) {</lineContent> | ||
</mutation> | ||
|
||
<mutation unstable="false"> | ||
<sourceFile>UnusedLocalVariableCheck.java</sourceFile> | ||
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck</mutatedClass> | ||
<mutatedMethod>isInsideLocalAnonInnerClass</mutatedMethod> | ||
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator> | ||
<description>removed conditional - replaced equality check with true</description> | ||
<lineContent>if (currentAst.getType() == TokenTypes.SLIST) {</lineContent> | ||
</mutation> | ||
</suppressedMutations> |
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
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
92 changes: 92 additions & 0 deletions
92
...heckstyle/checks/coding/unusedlocalvariable/InputUnusedLocalVariableLambdaExpression.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,92 @@ | ||
/* | ||
UnusedLocalVariable | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class InputUnusedLocalVariableLambdaExpression { | ||
private final LambdaTest<String> myComponent = LambdaTest.lazy(() -> { | ||
String foo = ""; | ||
String hoo = ""; // violation | ||
new Runnable() { | ||
String hoo = ""; | ||
|
||
@Override | ||
public void run() { | ||
String j = hoo; // violation | ||
String ja = foo; | ||
ja += "asd"; | ||
} | ||
}; | ||
return ""; | ||
}); | ||
|
||
final LambdaTest<String> nestedLambdas = LambdaTest.lazy(() -> { | ||
String foo = ""; | ||
String hoo = ""; | ||
String hoo2 = ""; // violation | ||
String hoo3 = ""; // violation | ||
final LambdaTest<String> myComponent = LambdaTest.lazy(() -> { // violation | ||
final LambdaTest<String> myComponent3 = LambdaTest.lazy(() -> { // violation | ||
new Runnable() { | ||
String hoo2 = ""; | ||
|
||
@Override | ||
public void run() { | ||
String j = hoo; // violation | ||
String ja = foo; | ||
ja += hoo2; | ||
} | ||
}; | ||
return ""; | ||
}); | ||
new Runnable() { | ||
String hoo3 = ""; | ||
|
||
@Override | ||
public void run() { | ||
String j = hoo3; // violation | ||
String ja = foo; | ||
ja += "asd"; | ||
} | ||
}; | ||
return ""; | ||
}); | ||
new Runnable() { | ||
String hoo2 = ""; | ||
|
||
@Override | ||
public void run() { | ||
String j = hoo2; | ||
String ja = foo; // violation | ||
j += hoo2; | ||
} | ||
}; | ||
return ""; | ||
}); | ||
|
||
final LambdaTest<String> nestedLambdas2 = LambdaTest.lazy(() -> { | ||
String k = ""; // violation | ||
final LambdaTest<String> nestedLambdas = LambdaTest.lazy(() -> { | ||
new LambdaTest<String>() { | ||
void foo() { | ||
String j = k; | ||
j += "asd"; | ||
} | ||
}; | ||
return ""; | ||
}); | ||
return nestedLambdas.toString(); | ||
}); | ||
} | ||
|
||
class LambdaTest<T> { | ||
String k = ""; | ||
public static <T> LambdaTest<T> lazy(Supplier<T> supplier) { | ||
return new LambdaTest<>(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ls/checkstyle/checks/coding/unusedlocalvariable/InputUnusedLocalVariableLocalClasses.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,26 @@ | ||
/* | ||
UnusedLocalVariable | ||
*/ | ||
|
||
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable; | ||
|
||
public class InputUnusedLocalVariableLocalClasses { | ||
|
||
int a = 12; | ||
|
||
void foo() { | ||
int a = 12; // violation | ||
int ab = 1; // violation | ||
|
||
class asd { | ||
InputUnusedLocalVariableLocalClasses a = new InputUnusedLocalVariableLocalClasses() { | ||
void asd() { | ||
System.out.println(a); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
} |