Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static com.google.errorprone.suppliers.Suppliers.typeFromClass;
import static com.google.errorprone.suppliers.Suppliers.typeFromString;
import static com.google.errorprone.util.ASTHelpers.findEnclosingMethod;
import static com.google.errorprone.util.ASTHelpers.findEnclosingNode;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
Expand Down Expand Up @@ -1132,16 +1131,7 @@ public static <T extends Tree> Matcher<T> inSynchronized() {
return true;
}

boolean enclosingMethodFix =
state
.errorProneOptions()
.getFlags()
.getBoolean("ASTHelpers:EnclosingMethodFix")
.orElse(true);
MethodTree methodTree =
enclosingMethodFix
? findEnclosingMethod(state)
: findEnclosingNode(state.getPath(), MethodTree.class);
MethodTree methodTree = findEnclosingMethod(state);
return methodTree != null
&& methodTree.getModifiers().getFlags().contains(Modifier.SYNCHRONIZED);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.google.common.base.CaseFormat;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.MustBeClosed;
Expand Down Expand Up @@ -77,20 +76,12 @@
import java.util.Optional;
import java.util.stream.Stream;
import javax.lang.model.element.ElementKind;
import org.jspecify.annotations.Nullable;

/**
* An abstract check for resources that must be closed; used by {@link StreamResourceLeak} and
* {@link MustBeClosedChecker}.
*/
public abstract class AbstractMustBeClosedChecker extends BugChecker {
private final boolean useAstHelpersEnclosingMethod;

protected AbstractMustBeClosedChecker(ErrorProneFlags flags) {
this.useAstHelpersEnclosingMethod =
flags.getBoolean("ASTHelpers:EnclosingMethodFix").orElse(true);
}

private static final String MUST_BE_CLOSED_ANNOTATION_NAME =
MustBeClosed.class.getCanonicalName();

Expand Down Expand Up @@ -257,8 +248,7 @@ protected boolean exemptChange(ExpressionTree tree, VisitorState state) {

private Optional<Change> checkClosed(
ExpressionTree tree, VisitorState state, NameSuggester suggester) {
MethodTree callerMethodTree =
useAstHelpersEnclosingMethod ? findEnclosingMethod(state) : buggyFindEnclosingMethod(state);
MethodTree callerMethodTree = findEnclosingMethod(state);
TreePath path = state.getPath();
OUTER:
while (true) {
Expand Down Expand Up @@ -364,28 +354,6 @@ private static Optional<Change> findingWithNoFix() {
return Change.of(SuggestedFix.emptyFix());
}

/**
* Returns the enclosing method of the given visitor state. Returns null if the state is within a
* lambda expression or anonymous class <b>or (bug!) other class-creation expression</b>.
*/
private static @Nullable MethodTree buggyFindEnclosingMethod(VisitorState state) {
for (Tree node : state.getPath().getParentPath()) {
switch (node) {
case LambdaExpressionTree let -> {
return null;
}
case NewClassTree nct -> {
return null;
}
case MethodTree methodTree -> {
return methodTree;
}
default -> {}
}
}
return null;
}

private static boolean isClosedInFinallyClause(VarSymbol var, TreePath path, VisitorState state) {
if (!isConsideredFinal(var)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher;
import com.google.errorprone.fixes.Fix;
Expand Down Expand Up @@ -60,7 +59,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;

Expand All @@ -71,14 +69,6 @@
+ " pattern.",
severity = ERROR)
public class DoubleBraceInitialization extends BugChecker implements NewClassTreeMatcher {

private final boolean enclosingMethodFix;

@Inject
DoubleBraceInitialization(ErrorProneFlags flags) {
this.enclosingMethodFix = flags.getBoolean("ASTHelpers:EnclosingMethodFix").orElse(true);
}

@SuppressWarnings("ImmutableEnumChecker") // Matcher is immutable in practice
enum CollectionTypes {
MAP("Map", "put", "ImmutableMap"),
Expand Down Expand Up @@ -110,8 +100,7 @@ enum CollectionTypes {
.named("unmodifiable" + type));
}

Optional<Fix> maybeFix(
NewClassTree tree, VisitorState state, BlockTree block, boolean enclosingMethodFix) {
Optional<Fix> maybeFix(NewClassTree tree, VisitorState state, BlockTree block) {
// scan the body for mutator methods (add, put) and record their arguments for rewriting as
// a static factory method
List<List<? extends ExpressionTree>> arguments = new ArrayList<>();
Expand Down Expand Up @@ -167,14 +156,9 @@ Optional<Fix> maybeFix(
}
if (enclosing instanceof ReturnTree returnTree) {
toReplace = returnTree.getExpression();
MethodTree enclosingMethod;
if (enclosingMethodFix) {
TreePath enclosingMethodPath = findEnclosingMethodPath(path);
enclosingMethod =
enclosingMethodPath == null ? null : (MethodTree) enclosingMethodPath.getLeaf();
} else {
enclosingMethod = ASTHelpers.findEnclosingNode(path, MethodTree.class);
}
TreePath enclosingMethodPath = findEnclosingMethodPath(path);
MethodTree enclosingMethod =
enclosingMethodPath == null ? null : (MethodTree) enclosingMethodPath.getLeaf();
typeTree = enclosingMethod == null ? null : enclosingMethod.getReturnType();
}
break;
Expand Down Expand Up @@ -251,10 +235,7 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
collectionType
.get()
.maybeFix(tree, state, block, enclosingMethodFix)
.ifPresent(description::addFix);
collectionType.get().maybeFix(tree, state, block).ifPresent(description::addFix);
return description.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static com.google.errorprone.util.AnnotationNames.MUST_BE_CLOSED_ANNOTATION;

import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
Expand All @@ -48,7 +47,6 @@
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import javax.inject.Inject;

/**
* Checks if a constructor or method annotated with {@link
Expand All @@ -65,11 +63,6 @@
severity = ERROR)
public class MustBeClosedChecker extends AbstractMustBeClosedChecker
implements MethodTreeMatcher, ClassTreeMatcher {
@Inject
MustBeClosedChecker(ErrorProneFlags flags) {
super(flags);
}

private static final Matcher<Tree> IS_AUTOCLOSEABLE = isSubtypeOf(AutoCloseable.class);

private static final Matcher<MethodTree> METHOD_RETURNS_AUTO_CLOSEABLE_MATCHER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@
import static com.sun.source.tree.Tree.Kind.NULL_LITERAL;

import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ReturnTreeMatcher;
import com.google.errorprone.dataflow.nullnesspropagation.TrustingNullnessAnalysis;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.util.TreePath;
import javax.inject.Inject;
import org.jspecify.annotations.Nullable;

/**
* Flags methods with collection return types which return {@code null} in some cases but don't
Expand All @@ -52,15 +47,6 @@
+ " annotate the method as @Nullable. See Effective Java 3rd Edition Item 54.",
severity = SUGGESTION)
public class ReturnsNullCollection extends BugChecker implements ReturnTreeMatcher {

private final boolean useAstHelpersEnclosingMethod;

@Inject
ReturnsNullCollection(ErrorProneFlags flags) {
this.useAstHelpersEnclosingMethod =
flags.getBoolean("ASTHelpers:EnclosingMethodFix").orElse(true);
}

private static boolean methodWithoutNullable(MethodTree tree, VisitorState state) {
return !TrustingNullnessAnalysis.hasNullableAnnotation(getSymbol(tree));
}
Expand All @@ -79,8 +65,7 @@ public final Description matchReturn(ReturnTree tree, VisitorState state) {
if (tree.getExpression() == null || tree.getExpression().getKind() != NULL_LITERAL) {
return NO_MATCH;
}
MethodTree methodTree =
useAstHelpersEnclosingMethod ? findEnclosingMethod(state) : buggyFindEnclosingMethod(state);
MethodTree methodTree = findEnclosingMethod(state);
if (methodTree == null) {
return NO_MATCH;
}
Expand All @@ -89,19 +74,4 @@ public final Description matchReturn(ReturnTree tree, VisitorState state) {
}
return describeMatch(tree);
}

/**
* Returns the enclosing method of the given visitor state. Returns null if the state is within a
* lambda expression or anonymous class <b>or (bug!) {@code catch} block</b>.
*/
private static @Nullable MethodTree buggyFindEnclosingMethod(VisitorState state) {
TreePath path = state.getPath();
while (path != null && path.getLeaf() instanceof StatementTree) {
path = path.getParentPath();
}
if (path == null || !(path.getLeaf() instanceof MethodTree)) {
return null;
}
return (MethodTree) path.getLeaf();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.errorprone.util.ASTHelpers.getStartPosition;

import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
Expand All @@ -45,7 +44,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
Expand All @@ -55,11 +53,6 @@
+ " try-with-resources",
severity = WARNING)
public class StreamResourceLeak extends AbstractMustBeClosedChecker implements MethodTreeMatcher {
@Inject
StreamResourceLeak(ErrorProneFlags flags) {
super(flags);
}

public static final Matcher<ExpressionTree> MATCHER =
MethodMatchers.staticMethod()
.onClass("java.nio.file.Files")
Expand Down
Loading