From c2e14f24facfae7a0e38e78dd4005f25c7f4f8b4 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Tue, 25 Jan 2022 09:07:40 -0800 Subject: [PATCH] Make `ASTHelpers.getSymbol(MethodInvocationTree)` and friends throw instead of return `null`. Fixes https://github.com/google/error-prone/issues/2882 PiperOrigin-RevId: 424097880 --- .../com/google/errorprone/util/ASTHelpers.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index fa9c3129b09..d97efb5c23a 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -297,10 +297,13 @@ public static MethodSymbol getSymbol(MethodTree tree) { } /** Gets the method symbol for a new class. */ - @Nullable public static MethodSymbol getSymbol(NewClassTree tree) { Symbol sym = ((JCNewClass) tree).constructor; - return sym instanceof MethodSymbol ? (MethodSymbol) sym : null; + if (!(sym instanceof MethodSymbol)) { + // Defensive. Would only occur if there are errors in the AST. + throw new IllegalArgumentException(tree.toString()); + } + return (MethodSymbol) sym; } /** Gets the symbol for a variable. */ @@ -309,21 +312,23 @@ public static VarSymbol getSymbol(VariableTree tree) { } /** Gets the symbol for a method invocation. */ - @Nullable public static MethodSymbol getSymbol(MethodInvocationTree tree) { Symbol sym = ASTHelpers.getSymbol(tree.getMethodSelect()); if (!(sym instanceof MethodSymbol)) { // Defensive. Would only occur if there are errors in the AST. - return null; + throw new IllegalArgumentException(tree.toString()); } return (MethodSymbol) sym; } /** Gets the symbol for a member reference. */ - @Nullable public static MethodSymbol getSymbol(MemberReferenceTree tree) { Symbol sym = ((JCMemberReference) tree).sym; - return sym instanceof MethodSymbol ? (MethodSymbol) sym : null; + if (!(sym instanceof MethodSymbol)) { + // Defensive. Would only occur if there are errors in the AST. + throw new IllegalArgumentException(tree.toString()); + } + return (MethodSymbol) sym; } /* Checks whether an expression requires parentheses. */