Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ private void f(Exception x) {
System.out.println("" + e);
}
}

// Unnamed exception variables
try {
} catch (Exception _) { // Compliant
}

try {
} catch (Exception _) { // Compliant
try {
} catch (Exception _) { // Compliant
}
}

try {
} catch (Exception _) { // Compliant
try {
} catch (Exception z) { // Noncompliant {{Either log or rethrow this exception.}}
}
}

try {
} catch (Exception e) { // Compliant
System.out.println(e);
try {
} catch (Exception _) { // Compliant
}
}
}

private void g() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ public void visitCatch(CatchTree tree) {
Symbol exception = tree.parameter().symbol();
usageStatusStack.addFirst(new UsageStatus(exception.usages()));
super.visitCatch(tree);
if (usageStatusStack.pop().isInvalid() && !exception.isUnknown()) {
if (usageStatusStack.pop().isInvalid() && !exception.isUnknown()
&& !isExceptionVariableUnnamed(tree.parameter())) {
context.reportIssue(this, tree.parameter(), "Either log or rethrow this exception.");
}
}
Expand Down Expand Up @@ -207,6 +208,10 @@ public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {

}

private boolean isExceptionVariableUnnamed(VariableTree parameter) {
return parameter.simpleName().isUnnamedVariable();
}

private boolean isExcludedType(Tree tree) {
if (tree.is(Kind.UNION_TYPE)) {
return ((UnionTypeTree) tree).typeAlternatives().stream().allMatch(this::isExcludedType);
Expand Down