From 4c76c1bad0379767e20c6c47626582beec84db3d Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 11 Aug 2022 20:00:49 +0200 Subject: [PATCH] Drop various vacuous null checks Recent versions of Error Prone guarantee that `ASTHelpers#getSymbol` never returns `null`. --- .../errorprone/bugpatterns/EmptyMethod.java | 4 +--- .../bugpatterns/JUnitMethodDeclaration.java | 3 +-- .../bugpatterns/PrimitiveComparison.java | 20 +++++++++---------- .../RefasterRuleCompilerTaskListener.java | 15 ++++---------- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EmptyMethod.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EmptyMethod.java index 9e74ee3844..9cd6ae0ece 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EmptyMethod.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/EmptyMethod.java @@ -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. */ @@ -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; } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitMethodDeclaration.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitMethodDeclaration.java index c02b764557..c342283008 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitMethodDeclaration.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitMethodDeclaration.java @@ -176,8 +176,7 @@ private static CharSequence getStaticImportSimpleName(Tree tree, VisitorState st } private static Optional 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)) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/PrimitiveComparison.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/PrimitiveComparison.java index 6a8788a8f6..4851db0b54 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/PrimitiveComparison.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/PrimitiveComparison.java @@ -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; @@ -84,16 +83,15 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState private static Optional 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)); } /** diff --git a/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/RefasterRuleCompilerTaskListener.java b/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/RefasterRuleCompilerTaskListener.java index eac10a01cc..9b80cd4b39 100644 --- a/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/RefasterRuleCompilerTaskListener.java +++ b/refaster-compiler/src/main/java/tech/picnic/errorprone/refaster/plugin/RefasterRuleCompilerTaskListener.java @@ -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; @@ -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(