Skip to content

Commit

Permalink
getThrownExceptions: handle try(var) {} form of try-with-resources too.
Browse files Browse the repository at this point in the history
Fixes external #3321

PiperOrigin-RevId: 464039917
  • Loading branch information
graememorgan authored and Error Prone Team committed Aug 1, 2022
1 parent 3dda6b2 commit 4a1a034
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Expand Up @@ -2157,7 +2157,6 @@ public static ImmutableSet<Type> getThrownExceptions(Tree tree, VisitorState sta

/** Scanner for determining what types are thrown by a tree. */
public static final class ScanThrownTypes extends TreeScanner<Void, Void> {
boolean inResources = false;
ArrayDeque<Set<Type>> thrownTypes = new ArrayDeque<>();
SetMultimap<VarSymbol, Type> thrownTypesByVariable = HashMultimap.create();

Expand Down Expand Up @@ -2215,9 +2214,15 @@ public Void visitTry(TryTree tree, Void unused) {
}

public void scanResources(TryTree tree) {
inResources = true;
for (Tree resource : tree.getResources()) {
Symbol symbol = getType(resource).tsym;

if (symbol instanceof ClassSymbol) {
getCloseMethod((ClassSymbol) symbol, state)
.ifPresent(methodSymbol -> getThrownTypes().addAll(methodSymbol.getThrownTypes()));
}
}
scan(tree.getResources(), null);
inResources = false;
}

@Override
Expand All @@ -2241,13 +2246,6 @@ public Void visitNewClass(NewClassTree tree, Void unused) {

@Override
public Void visitVariable(VariableTree tree, Void unused) {
if (inResources) {
Symbol symbol = getSymbol(tree.getType());
if (symbol instanceof ClassSymbol) {
getCloseMethod((ClassSymbol) symbol, state)
.ifPresent(methodSymbol -> getThrownTypes().addAll(methodSymbol.getThrownTypes()));
}
}
return super.visitVariable(tree, null);
}

Expand Down

0 comments on commit 4a1a034

Please sign in to comment.