Fix unused exception in loop

This commit is contained in:
Clément Fournier
2020-06-28 16:35:27 +02:00
parent 105f1a13a1
commit 388e29a9de
3 changed files with 38 additions and 2 deletions

View File

@ -101,4 +101,10 @@ public class ASTCatchStatement extends AbstractJavaNode {
return getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage();
}
/**
* Returns the declarator id for the exception parameter.
*/
public ASTVariableDeclaratorId getExceptionId() {
return getFirstChildOfType(ASTFormalParameter.class).getVariableDeclaratorId();
}
}

View File

@ -108,7 +108,6 @@ public class UnusedAssignmentRule extends AbstractJavaRule {
or at least proper graph algorithms like toposort)
-> this is pretty invisible as it causes false negatives, not FPs
* test ternary expr
* conditional exprs in loops
DONE
* conditionals
@ -124,6 +123,7 @@ public class UnusedAssignmentRule extends AbstractJavaRule {
* test local class/anonymous class
* shortcut conditionals have their own control-flow
* parenthesized expressions
* conditional exprs in loops
*/
@ -543,6 +543,13 @@ public class UnusedAssignmentRule extends AbstractJavaRule {
return finalState;
}
@Override
public Object visit(ASTCatchStatement node, Object data) {
SpanInfo result = (SpanInfo) visit((JavaNode) node, data);
result.deleteVar(node.getExceptionId().getNameDeclaration());
return result;
}
@Override
public Object visit(ASTLambdaExpression node, Object data) {
// Lambda expression have control flow that is separate from the method
@ -596,7 +603,6 @@ public class UnusedAssignmentRule extends AbstractJavaRule {
JavaNode body,
boolean checkFirstIter,
VariableNameDeclaration foreachVar) {
// TODO linkConditional
final GlobalAlgoState globalState = before.global;
SpanInfo breakTarget = before.forkEmpty();

View File

@ -2509,4 +2509,28 @@ class Foo {
</test-code>
<test-code>
<description>Catch in loop</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static unknown.K.*;
class Foo {
{
for (String s : someExpression()) {
try {
foo(s);
} catch (Exception e) {
print("failure");
}
}
}
}
]]></code>
</test-code>
</test-data>