Skip to content

Commit

Permalink
SONARJAVA-4406 also considers throw statements
Browse files Browse the repository at this point in the history
  • Loading branch information
kaufco authored and leveretka committed Feb 20, 2023
1 parent b1109b5 commit ef8feb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,29 @@ void bar() throws InterruptedException {
}
}

public void throwExceptionInTryDirectly() throws InterruptedException {
try {
throw new RuntimeException();
} catch (Exception e) { // Compliant
}

try {
throw new InterruptedException();
} catch (Exception e) { // Noncompliant
}

try {
InterruptedException ie = new InterruptedException();
throw ie;
} catch (Exception e) { // Noncompliant
}

try {
throw getInterruptedException();
} catch (Exception e) { // Noncompliant
}
}

void falsePositivesSonarjava4406() {
try {
try {
Expand All @@ -381,6 +404,15 @@ void falsePositivesSonarjava4406() {
} catch (Exception e) { // Compliant, because inner try does not throw an InterruptedException
}

try {
try {
throwsInterruptedException();
} catch (InterruptedException ie) { // Compliant
throw new InterruptedException();
}
} catch (Exception e) { // Noncompliant, because inner try throws an InterruptedException
}

try {
try {
throwsInterruptedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ public void visitTryStatement(TryStatementTree tryStatementTree) {
scan(tryStatementTree.catches());
scan(tryStatementTree.finallyBlock());
}

@Override
public void visitThrowStatement(ThrowStatementTree tree) {
// besides to method invocation, we also need to collect throw statements
if (isInterruptingExceptionExpression(tree.expression())) {
invocationTree.add(tree);
}
super.visitThrowStatement(tree);
}
}

private static class BlockVisitor extends BaseTreeVisitor {
Expand Down

0 comments on commit ef8feb7

Please sign in to comment.