Skip to content

Commit

Permalink
Tweak ThrowIfUncheckedKnownChecked implementation to match `ThrowIf…
Browse files Browse the repository at this point in the history
…UncheckedKnownUnchecked`.

(followup to unknown commit)

PiperOrigin-RevId: 686902095
  • Loading branch information
cpovirk authored and Error Prone Team committed Oct 17, 2024
1 parent 6380cc2 commit 40bb976
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.UnionType;

/** Catches no-op calls to {@code Throwables.throwIfUnchecked}. */
/** A BugPattern; see the summary. */
@BugPattern(summary = "throwIfUnchecked(knownCheckedException) is a no-op.", severity = ERROR)
public class ThrowIfUncheckedKnownChecked extends BugChecker
implements MethodInvocationTreeMatcher {
Expand All @@ -57,12 +56,8 @@ public class ThrowIfUncheckedKnownChecked extends BugChecker
public boolean matches(ExpressionTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
if (type.isUnion()) {
for (TypeMirror alternative : ((UnionType) type).getAlternatives()) {
if (!isKnownCheckedException(state, (Type) alternative)) {
return false;
}
}
return true;
return ((UnionType) type)
.getAlternatives().stream().allMatch(t -> isKnownCheckedException(state, (Type) t));
} else {
return isKnownCheckedException(state, type);
}
Expand All @@ -72,6 +67,7 @@ boolean isKnownCheckedException(VisitorState state, Type type) {
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
// Check erasure for generics.
// TODO(cpovirk): Is that necessary here or in ThrowIfUncheckedKnownUnchecked?
type = types.erasure(type);
return
// Has to be some Exception: A variable of type Throwable might be an Error.
Expand Down

0 comments on commit 40bb976

Please sign in to comment.