Skip to content

Commit

Permalink
Remove some more redundant null checks.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 439640674
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 5, 2022
1 parent fba919b commit fe05664
Show file tree
Hide file tree
Showing 54 changed files with 44 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ private static Matcher<MethodTree> overridesMethodOfClass(Class<?> clazz) {
@Override
public boolean matches(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
if (symbol == null) {
return false;
}
for (MethodSymbol superMethod : findSuperMethods(symbol, state.getTypes())) {
if (superMethod.owner != null
&& superMethod.owner.getQualifiedName().contentEquals(clazz.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return NO_MATCH;
}
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
if (!ASTHelpers.isSubtype(sym.owner.asType(), CACHE_LOADER_TYPE.get(state), state)) {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ClassCanBeStatic extends BugChecker implements ClassTreeMatcher {
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol currentClass = ASTHelpers.getSymbol(tree);
if (currentClass == null || !currentClass.hasOuterInstance()) {
if (!currentClass.hasOuterInstance()) {
return NO_MATCH;
}
if (currentClass.getNestingKind() != NestingKind.MEMBER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
@Override
public Description matchMethod(MethodTree node, VisitorState state) {
Symbol.MethodSymbol method = ASTHelpers.getSymbol(node);
if (method == null) {
return Description.NO_MATCH;
}
List<Integer> compileTimeConstantAnnotationIndexes =
getAnnotatedParams(method.getParameters(), state);
if (compileTimeConstantAnnotationIndexes.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ConstantField extends BugChecker implements VariableTreeMatcher {
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Symbol.VarSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || sym.getKind() != ElementKind.FIELD) {
if (sym.getKind() != ElementKind.FIELD) {
return Description.NO_MATCH;
}
String name = sym.getSimpleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
if (outerMethodTree == null) {
return NO_MATCH;
}
Symbol sym = getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
VarSymbol sym = getSymbol(tree);
switch (sym.getKind()) {
case RESOURCE_VARIABLE:
return describeMatch(tree);
Expand All @@ -115,8 +112,7 @@ private static SuggestedFix fixLocal(
}
MethodSymbol methodSymbol = getSymbol(outerMethodTree);
boolean canUseStatic =
(methodSymbol != null
&& methodSymbol.owner.enclClass().getNestingKind() == NestingKind.TOP_LEVEL)
methodSymbol.owner.enclClass().getNestingKind() == NestingKind.TOP_LEVEL
|| outerMethodTree.getModifiers().getFlags().contains(Modifier.STATIC);
String replacement =
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return NO_MATCH;
}
VarSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || sym.getKind() != ElementKind.FIELD) {
if (sym.getKind() != ElementKind.FIELD) {
return NO_MATCH;
}
String name = sym.getSimpleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ private void replaceLiteral(LiteralTree tree, Scope scope, VisitorState state) {

private void saveConstValue(VariableTree tree, Scope scope) {
VarSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return;
}
if (!isConsideredFinal(sym)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ public class DoNotCallChecker extends BugChecker
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
if (hasAnnotation(tree, DO_NOT_CALL, state)) {
if (symbol.getModifiers().contains(Modifier.PRIVATE)) {
return buildDescription(tree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ public class DoNotCallSuggester extends BugChecker implements MethodTreeMatcher
public Description matchMethod(MethodTree tree, VisitorState state) {
// if we can't find the method symbol, exit
MethodSymbol symbol = getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}

// if the method is abstract, exit
if (tree.getBody() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}
ClassSymbol classSymbol = getSymbol(classTree);
if (classSymbol == null || classSymbol.isAnonymous()) {
if (classSymbol.isAnonymous()) {
return Description.NO_MATCH;
}
MethodTree methodTree = (MethodTree) methodTreePath.getLeaf();
Expand All @@ -97,8 +97,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
VariableTree parameter = getOnlyElement(methodTree.getParameters());
ExpressionTree receiver = getReceiver(tree);
VarSymbol symbol = getSymbol(parameter);
if (symbol == null
|| receiver == null
if (receiver == null
|| receiver.getKind() != Kind.IDENTIFIER
|| !symbol.equals(getSymbol(receiver))) {
return Description.NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private FinalScanner(VariableAssignmentRecords writes, VisitorState compilationS
@Override
public Void visitVariable(VariableTree node, InitializationContext init) {
VarSymbol sym = ASTHelpers.getSymbol(node);
if (sym != null && sym.getKind() == ElementKind.FIELD && !isSuppressed(node)) {
if (sym.getKind() == ElementKind.FIELD && !isSuppressed(node)) {
writes.recordDeclaration(sym, node);
}
return super.visitVariable(node, InitializationContext.NONE);
Expand All @@ -281,7 +281,7 @@ public Void visitBlock(BlockTree node, InitializationContext init) {
@Override
public Void visitMethod(MethodTree node, InitializationContext init) {
MethodSymbol sym = ASTHelpers.getSymbol(node);
if (sym != null && sym.isConstructor()) {
if (sym.isConstructor()) {
init = InitializationContext.INSTANCE;
}
return super.visitMethod(node, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
@Override
public Void visitVariable(VariableTree variableTree, Void unused) {
VarSymbol symbol = getSymbol(variableTree);
if (symbol != null
&& symbol.getKind() == ElementKind.FIELD
if (symbol.getKind() == ElementKind.FIELD
&& symbol.isPrivate()
&& canBeLocal(variableTree)
&& !shouldKeep(variableTree)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public FieldCanBeStatic(ErrorProneFlags flags) {
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
VarSymbol symbol = getSymbol(tree);
if (symbol == null
|| !canBeRemoved(symbol)
if (!canBeRemoved(symbol)
|| !tree.getModifiers().getFlags().contains(FINAL)
|| symbol.isStatic()
|| !symbol.getKind().equals(FIELD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static java.util.stream.Collectors.toCollection;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.code.Symbol;
Expand Down Expand Up @@ -58,14 +58,10 @@ public Description matchClass(ClassTree classTree, VisitorState visitorState) {
classTree.getMembers().stream()
.filter(mem -> mem instanceof VariableTree)
.map(mem -> (VariableTree) mem)
.filter(
mem ->
!isSuppressed(ASTHelpers.getSymbol(mem))
&& !isIgnoredType(mem)
&& !isStatic(mem))
.filter(mem -> !isSuppressed(getSymbol(mem)) && !isIgnoredType(mem) && !isStatic(mem))
.collect(toCollection(ArrayList::new));

ClassSymbol classSymbol = ASTHelpers.getSymbol(classTree);
ClassSymbol classSymbol = getSymbol(classTree);

while (!classSymbol.getSuperclass().getKind().equals(TypeKind.NONE)) {
TypeSymbol parentSymbol = classSymbol.getSuperclass().asElement();
Expand Down Expand Up @@ -123,13 +119,7 @@ private void checkForHiddenFields(
}

private static boolean isIgnoredType(VariableTree variableTree) {
VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree);

if (varSymbol != null) { // varSymbol is null when variable is primitive type
return IGNORED_CLASSES.contains(varSymbol.getQualifiedName().toString());
}

return false;
return IGNORED_CLASSES.contains(getSymbol(variableTree).getQualifiedName().toString());
}

private static boolean isStatic(VariableTree varTree) {
Expand All @@ -142,7 +132,7 @@ private static boolean isPackagePrivateAndInDiffPackage(
&& !parentVariable.getModifiers().contains(Modifier.PROTECTED)
&& !parentVariable.getModifiers().contains(Modifier.PUBLIC)) { // package-private variable

if (!parentVariable.packge().equals(ASTHelpers.getSymbol(currClass).packge())) {
if (!parentVariable.packge().equals(getSymbol(currClass).packge())) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected MethodInvocationTree defaultAction(Tree tree, Void unused) {
}
ExpressionTree select = invocation.getMethodSelect();
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || !sym.equals(ASTHelpers.getSymbol(invocation))) {
if (!sym.equals(ASTHelpers.getSymbol(invocation))) {
return NO_MATCH;
}
if (!sym.isStatic()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return NO_MATCH;
}
VarSymbol symbol = getSymbol(tree);
if (symbol == null || !isConsideredFinal(symbol)) {
if (!isConsideredFinal(symbol)) {
return NO_MATCH;
}
List<TreePath> assignments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
}
List<? extends Tree> members = tree.getMembers();
ClassSymbol symbol = getSymbol(tree);
if (symbol == null || !symbol.isInterface() || symbol.isAnnotationType()) {
if (!symbol.isInterface() || symbol.isAnnotationType()) {
return Description.NO_MATCH;
}
int staticMembers = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class IterablePathParameter extends BugChecker implements VariableTreeMat
public Description matchVariable(VariableTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
VarSymbol symbol = ASTHelpers.getSymbol(tree);
if (type == null || symbol == null) {
if (type == null) {
return NO_MATCH;
}
if (symbol.getKind() != ElementKind.PARAMETER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ public Description matchClass(ClassTree tree, VisitorState state) {
return NO_MATCH;
}
ClassSymbol symbol = ASTHelpers.getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
return describeIfObsolete(null, state.getTypes().directSupertypes(symbol.asType()), state);
}

Expand Down Expand Up @@ -416,9 +413,6 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
private static boolean implementingObsoleteMethod(
MethodTree enclosingMethod, VisitorState state, Type type) {
MethodSymbol method = ASTHelpers.getSymbol(enclosingMethod);
if (method == null) {
return false;
}
if (ASTHelpers.findSuperMethods(method, state.getTypes()).isEmpty()) {
// not an override
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public final class MemberName extends BugChecker implements MethodTreeMatcher, V
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
if (!annotationsAmong(symbol.owner, EXEMPTED_CLASS_ANNOTATIONS.get(state), state).isEmpty()) {
return NO_MATCH;
}
Expand Down Expand Up @@ -138,9 +135,6 @@ private static boolean hasTestAnnotation(MethodSymbol symbol) {
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
VarSymbol symbol = getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
String name = tree.getName().toString();
// Try to avoid dual-matching with ConstantCaseForConstants.
if (UPPER_UNDERSCORE_PATTERN.matcher(name).matches() && !symbol.isStatic()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ private void fixQualifier(Tree tree, ExpressionTree qualifierExpression) {

private static boolean isExcluded(MethodTree tree, VisitorState state) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return true;
}
if (sym.isConstructor() || !disjoint(EXCLUDED_MODIFIERS, sym.getModifiers())) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
}

MethodSymbol methodSym = ASTHelpers.getSymbol(methodTree);
if (methodSym == null) {
return Description.NO_MATCH;
}

if (!methodSym.getModifiers().contains(Modifier.ABSTRACT)) {
return Description.NO_MATCH;
Expand All @@ -131,9 +128,6 @@ public Description matchAnnotation(AnnotationTree tree, VisitorState state) {
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol methodSym = ASTHelpers.getSymbol(tree);
if (methodSym == null) {
return Description.NO_MATCH;
}

// Allow abstract methods.
if (methodSym.getModifiers().contains(Modifier.ABSTRACT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,6 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return NO_MATCH;
}
MethodSymbol symbol = ASTHelpers.getSymbol(tree);
if (symbol == null) {
return NO_MATCH;
}
return findSuperMethods(symbol, state.getTypes()).stream()
.filter(s -> ASTHelpers.hasAnnotation(s, NoAllocation.class.getName(), state))
.findAny()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
}
for (VariableTree parameter : tree.getParameters()) {
VarSymbol sym = ASTHelpers.getSymbol(parameter);
if (sym == null) {
continue;
}
if (!CompileTimeConstantExpressionMatcher.hasCompileTimeConstantAnnotation(state, sym)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public class NullableConstructor extends BugChecker implements MethodTreeMatcher
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
if (!sym.isConstructor()) {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import java.util.List;
Expand All @@ -56,19 +55,12 @@ public Description matchAnnotatedType(AnnotatedTypeTree tree, VisitorState state
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
return check(sym.getReturnType(), tree.getModifiers().getAnnotations());
}

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Symbol.VarSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
return check(sym.type, tree.getModifiers().getAnnotations());
return check(ASTHelpers.getSymbol(tree).type, tree.getModifiers().getAnnotations());
}

private Description check(Type type, List<? extends AnnotationTree> annotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public class NullableVoid extends BugChecker implements MethodTreeMatcher {
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
if (sym.getReturnType().getKind() != TypeKind.VOID) {
return NO_MATCH;
}
Expand Down
Loading

0 comments on commit fe05664

Please sign in to comment.