Skip to content

Commit

Permalink
Drop various vacuous null checks (#191)
Browse files Browse the repository at this point in the history
Recent versions of Error Prone guarantee that `ASTHelpers#getSymbol`
never returns `null`.
  • Loading branch information
Stephan202 authored Aug 18, 2022
1 parent 130c3d0 commit ef751ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.Optional;

/** A {@link BugChecker} which flags empty methods that seemingly can simply be deleted. */
Expand All @@ -47,8 +46,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return Description.NO_MATCH;
}

MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null || ASTHelpers.methodCanBeOverridden(sym)) {
if (ASTHelpers.methodCanBeOverridden(ASTHelpers.getSymbol(tree))) {
return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ private static CharSequence getStaticImportSimpleName(Tree tree, VisitorState st
}

private static Optional<String> tryCanonicalizeMethodName(MethodTree tree) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(sym -> sym.getQualifiedName().toString())
return Optional.of(ASTHelpers.getSymbol(tree).getQualifiedName().toString())
.filter(name -> name.startsWith(TEST_PREFIX))
.map(name -> name.substring(TEST_PREFIX.length()))
.filter(not(String::isEmpty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;

import com.google.auto.service.AutoService;
Expand Down Expand Up @@ -84,16 +83,15 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

private static Optional<Fix> attemptMethodInvocationReplacement(
MethodInvocationTree tree, Type cmpType, boolean isStatic, VisitorState state) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(methodSymbol -> methodSymbol.getSimpleName().toString())
.flatMap(
actualMethodName ->
Optional.of(getPreferredMethod(cmpType, isStatic, state))
.filter(not(actualMethodName::equals)))
.map(
preferredMethodName ->
prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state))
.map(preferredMethodName -> suggestFix(tree, preferredMethodName, state));
String actualMethodName = ASTHelpers.getSymbol(tree).getSimpleName().toString();
String preferredMethodName = getPreferredMethod(cmpType, isStatic, state);
if (actualMethodName.equals(preferredMethodName)) {
return Optional.empty();
}

return Optional.of(
suggestFix(
tree, prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state), state));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
Expand Down Expand Up @@ -103,16 +102,10 @@ public Void visitClass(ClassTree node, Void v) {
}

private FileObject getOutputFile(TaskEvent taskEvent, ClassTree tree) throws IOException {
String packageName =
Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(ASTHelpers::enclosingPackage)
.map(PackageSymbol::toString)
.orElse("");
CharSequence className =
Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(RefasterRuleCompilerTaskListener::toSimpleFlatName)
.orElseGet(tree::getSimpleName);
String relativeName = className + ".refaster";
ClassSymbol symbol = ASTHelpers.getSymbol(tree);
PackageSymbol enclosingPackage = ASTHelpers.enclosingPackage(symbol);
String packageName = enclosingPackage == null ? "" : enclosingPackage.toString();
String relativeName = toSimpleFlatName(symbol) + ".refaster";

JavaFileManager fileManager = context.get(JavaFileManager.class);
return fileManager.getFileForOutput(
Expand Down

0 comments on commit ef751ce

Please sign in to comment.