diff --git a/check_api/src/main/java/com/google/errorprone/bugpatterns/BugChecker.java b/check_api/src/main/java/com/google/errorprone/bugpatterns/BugChecker.java index b553381082e..0fdae63adb7 100644 --- a/check_api/src/main/java/com/google/errorprone/bugpatterns/BugChecker.java +++ b/check_api/src/main/java/com/google/errorprone/bugpatterns/BugChecker.java @@ -43,6 +43,7 @@ import com.sun.source.tree.AssertTree; import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BinaryTree; +import com.sun.source.tree.BindingPatternTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.BreakTree; import com.sun.source.tree.CaseTree; @@ -55,6 +56,7 @@ import com.sun.source.tree.DoWhileLoopTree; import com.sun.source.tree.EmptyStatementTree; import com.sun.source.tree.EnhancedForLoopTree; +import com.sun.source.tree.ExportsTree; import com.sun.source.tree.ExpressionStatementTree; import com.sun.source.tree.ForLoopTree; import com.sun.source.tree.IdentifierTree; @@ -70,12 +72,18 @@ import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.ModifiersTree; +import com.sun.source.tree.ModuleTree; import com.sun.source.tree.NewArrayTree; import com.sun.source.tree.NewClassTree; +import com.sun.source.tree.OpensTree; +import com.sun.source.tree.PackageTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.PrimitiveTypeTree; +import com.sun.source.tree.ProvidesTree; +import com.sun.source.tree.RequiresTree; import com.sun.source.tree.ReturnTree; +import com.sun.source.tree.SwitchExpressionTree; import com.sun.source.tree.SwitchTree; import com.sun.source.tree.SynchronizedTree; import com.sun.source.tree.ThrowTree; @@ -85,9 +93,11 @@ import com.sun.source.tree.TypeParameterTree; import com.sun.source.tree.UnaryTree; import com.sun.source.tree.UnionTypeTree; +import com.sun.source.tree.UsesTree; import com.sun.source.tree.VariableTree; import com.sun.source.tree.WhileLoopTree; import com.sun.source.tree.WildcardTree; +import com.sun.source.tree.YieldTree; import com.sun.source.util.TreePath; import com.sun.source.util.TreePathScanner; import com.sun.source.util.TreeScanner; @@ -342,14 +352,14 @@ public Void scan(Tree tree, Void unused) { return ImmutableRangeSet.copyOf(suppressedRegions); } - public interface AnnotationTreeMatcher extends Suppressible { - Description matchAnnotation(AnnotationTree tree, VisitorState state); - } - public interface AnnotatedTypeTreeMatcher extends Suppressible { Description matchAnnotatedType(AnnotatedTypeTree tree, VisitorState state); } + public interface AnnotationTreeMatcher extends Suppressible { + Description matchAnnotation(AnnotationTree tree, VisitorState state); + } + public interface ArrayAccessTreeMatcher extends Suppressible { Description matchArrayAccess(ArrayAccessTree tree, VisitorState state); } @@ -370,6 +380,10 @@ public interface BinaryTreeMatcher extends Suppressible { Description matchBinary(BinaryTree tree, VisitorState state); } + public interface BindingPatternTreeMatcher extends Suppressible { + Description matchBindingPattern(BindingPatternTree tree, VisitorState state); + } + public interface BlockTreeMatcher extends Suppressible { Description matchBlock(BlockTree tree, VisitorState state); } @@ -420,6 +434,10 @@ public interface EnhancedForLoopTreeMatcher extends Suppressible { // Intentionally skip ErroneousTreeMatcher -- we don't analyze malformed expressions. + public interface ExportsTreeMatcher extends Suppressible { + Description matchExports(ExportsTree tree, VisitorState state); + } + public interface ExpressionStatementTreeMatcher extends Suppressible { Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state); } @@ -480,6 +498,10 @@ public interface ModifiersTreeMatcher extends Suppressible { Description matchModifiers(ModifiersTree tree, VisitorState state); } + public interface ModuleTreeMatcher extends Suppressible { + Description matchModule(ModuleTree tree, VisitorState state); + } + public interface NewArrayTreeMatcher extends Suppressible { Description matchNewArray(NewArrayTree tree, VisitorState state); } @@ -491,6 +513,14 @@ public interface NewClassTreeMatcher extends Suppressible { // Intentionally skip OtherTreeMatcher. It seems to be used only for let expressions, which are // generated by javac to implement autoboxing. We are only interested in source-level constructs. + public interface OpensTreeMatcher extends Suppressible { + Description matchOpens(OpensTree tree, VisitorState state); + } + + public interface PackageTreeMatcher extends Suppressible { + Description matchPackage(PackageTree tree, VisitorState state); + } + public interface ParameterizedTypeTreeMatcher extends Suppressible { Description matchParameterizedType(ParameterizedTypeTree tree, VisitorState state); } @@ -503,10 +533,22 @@ public interface PrimitiveTypeTreeMatcher extends Suppressible { Description matchPrimitiveType(PrimitiveTypeTree tree, VisitorState state); } + public interface ProvidesTreeMatcher extends Suppressible { + Description matchProvides(ProvidesTree tree, VisitorState state); + } + + public interface RequiresTreeMatcher extends Suppressible { + Description matchRequires(RequiresTree tree, VisitorState state); + } + public interface ReturnTreeMatcher extends Suppressible { Description matchReturn(ReturnTree tree, VisitorState state); } + public interface SwitchExpressionTreeMatcher extends Suppressible { + Description matchSwitchExpression(SwitchExpressionTree tree, VisitorState state); + } + public interface SwitchTreeMatcher extends Suppressible { Description matchSwitch(SwitchTree tree, VisitorState state); } @@ -539,6 +581,10 @@ public interface UnionTypeTreeMatcher extends Suppressible { Description matchUnionType(UnionTypeTree tree, VisitorState state); } + public interface UsesTreeMatcher extends Suppressible { + Description matchUses(UsesTree tree, VisitorState state); + } + public interface VariableTreeMatcher extends Suppressible { Description matchVariable(VariableTree tree, VisitorState state); } @@ -551,6 +597,10 @@ public interface WildcardTreeMatcher extends Suppressible { Description matchWildcard(WildcardTree tree, VisitorState state); } + public interface YieldTreeMatcher extends Suppressible { + Description matchYield(YieldTree tree, VisitorState state); + } + @Override public boolean equals(Object obj) { if (!(obj instanceof BugChecker)) { diff --git a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneScanner.java b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneScanner.java index 90eb6a63243..b6a2960d042 100644 --- a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneScanner.java +++ b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneScanner.java @@ -32,6 +32,7 @@ import com.google.errorprone.bugpatterns.BugChecker.AssertTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.AssignmentTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.BindingPatternTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.BlockTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.BreakTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.CaseTreeMatcher; @@ -44,6 +45,7 @@ import com.google.errorprone.bugpatterns.BugChecker.DoWhileLoopTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.EmptyStatementTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.EnhancedForLoopTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.ExportsTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ExpressionStatementTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ForLoopTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.IdentifierTreeMatcher; @@ -59,12 +61,18 @@ import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ModifiersTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.ModuleTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.NewArrayTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.OpensTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.PackageTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ParameterizedTypeTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ParenthesizedTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.PrimitiveTypeTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.ProvidesTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.RequiresTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ReturnTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.SwitchExpressionTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.SynchronizedTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.ThrowTreeMatcher; @@ -73,9 +81,11 @@ import com.google.errorprone.bugpatterns.BugChecker.TypeParameterTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.UnaryTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.UnionTypeTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.UsesTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.WhileLoopTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.WildcardTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.YieldTreeMatcher; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Suppressible; import com.google.errorprone.util.ASTHelpers; @@ -86,6 +96,7 @@ import com.sun.source.tree.AssertTree; import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BinaryTree; +import com.sun.source.tree.BindingPatternTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.BreakTree; import com.sun.source.tree.CaseTree; @@ -98,6 +109,7 @@ import com.sun.source.tree.DoWhileLoopTree; import com.sun.source.tree.EmptyStatementTree; import com.sun.source.tree.EnhancedForLoopTree; +import com.sun.source.tree.ExportsTree; import com.sun.source.tree.ExpressionStatementTree; import com.sun.source.tree.ForLoopTree; import com.sun.source.tree.IdentifierTree; @@ -113,12 +125,18 @@ import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.ModifiersTree; +import com.sun.source.tree.ModuleTree; import com.sun.source.tree.NewArrayTree; import com.sun.source.tree.NewClassTree; +import com.sun.source.tree.OpensTree; +import com.sun.source.tree.PackageTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.PrimitiveTypeTree; +import com.sun.source.tree.ProvidesTree; +import com.sun.source.tree.RequiresTree; import com.sun.source.tree.ReturnTree; +import com.sun.source.tree.SwitchExpressionTree; import com.sun.source.tree.SwitchTree; import com.sun.source.tree.SynchronizedTree; import com.sun.source.tree.ThrowTree; @@ -128,9 +146,11 @@ import com.sun.source.tree.TypeParameterTree; import com.sun.source.tree.UnaryTree; import com.sun.source.tree.UnionTypeTree; +import com.sun.source.tree.UsesTree; import com.sun.source.tree.VariableTree; import com.sun.source.tree.WhileLoopTree; import com.sun.source.tree.WildcardTree; +import com.sun.source.tree.YieldTree; import com.sun.source.util.TreePath; import com.sun.tools.javac.code.Symbol.CompletionFailure; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; @@ -213,13 +233,14 @@ protected Set getCustomSuppressionAnnotations(VisitorState state return customSuppressionAnnotations.get(state); } - private final List annotationMatchers = new ArrayList<>(); private final List annotatedTypeMatchers = new ArrayList<>(); + private final List annotationMatchers = new ArrayList<>(); private final List arrayAccessMatchers = new ArrayList<>(); private final List arrayTypeMatchers = new ArrayList<>(); private final List assertMatchers = new ArrayList<>(); private final List assignmentMatchers = new ArrayList<>(); private final List binaryMatchers = new ArrayList<>(); + private final List bindingPatternMatchers = new ArrayList<>(); private final List blockMatchers = new ArrayList<>(); private final List breakMatchers = new ArrayList<>(); private final List caseMatchers = new ArrayList<>(); @@ -233,6 +254,7 @@ protected Set getCustomSuppressionAnnotations(VisitorState state private final List doWhileLoopMatchers = new ArrayList<>(); private final List emptyStatementMatchers = new ArrayList<>(); private final List enhancedForLoopMatchers = new ArrayList<>(); + private final List exportsMatchers = new ArrayList<>(); private final List expressionStatementMatchers = new ArrayList<>(); private final List forLoopMatchers = new ArrayList<>(); @@ -249,12 +271,18 @@ protected Set getCustomSuppressionAnnotations(VisitorState state private final List methodMatchers = new ArrayList<>(); private final List methodInvocationMatchers = new ArrayList<>(); private final List modifiersMatchers = new ArrayList<>(); + private final List moduleMatchers = new ArrayList<>(); private final List newArrayMatchers = new ArrayList<>(); private final List newClassMatchers = new ArrayList<>(); + private final List opensMatchers = new ArrayList<>(); + private final List packageMatchers = new ArrayList<>(); private final List parameterizedTypeMatchers = new ArrayList<>(); private final List parenthesizedMatchers = new ArrayList<>(); private final List primitiveTypeMatchers = new ArrayList<>(); + private final List providesMatchers = new ArrayList<>(); + private final List requiresMatchers = new ArrayList<>(); private final List returnMatchers = new ArrayList<>(); + private final List switchExpressionMatchers = new ArrayList<>(); private final List switchMatchers = new ArrayList<>(); private final List synchronizedMatchers = new ArrayList<>(); private final List throwMatchers = new ArrayList<>(); @@ -263,21 +291,23 @@ protected Set getCustomSuppressionAnnotations(VisitorState state private final List typeParameterMatchers = new ArrayList<>(); private final List unaryMatchers = new ArrayList<>(); private final List unionTypeMatchers = new ArrayList<>(); + private final List usesMatchers = new ArrayList<>(); private final List variableMatchers = new ArrayList<>(); private final List whileLoopMatchers = new ArrayList<>(); private final List wildcardMatchers = new ArrayList<>(); + private final List yieldMatchers = new ArrayList<>(); private void registerNodeTypes( BugChecker checker, ImmutableSet.Builder> customSuppressionAnnotationClasses) { customSuppressionAnnotationClasses.addAll(checker.customSuppressionAnnotations()); - if (checker instanceof AnnotationTreeMatcher) { - annotationMatchers.add((AnnotationTreeMatcher) checker); - } if (checker instanceof AnnotatedTypeTreeMatcher) { annotatedTypeMatchers.add((AnnotatedTypeTreeMatcher) checker); } + if (checker instanceof AnnotationTreeMatcher) { + annotationMatchers.add((AnnotationTreeMatcher) checker); + } if (checker instanceof ArrayAccessTreeMatcher) { arrayAccessMatchers.add((ArrayAccessTreeMatcher) checker); } @@ -293,6 +323,9 @@ private void registerNodeTypes( if (checker instanceof BinaryTreeMatcher) { binaryMatchers.add((BinaryTreeMatcher) checker); } + if (checker instanceof BindingPatternTreeMatcher) { + bindingPatternMatchers.add((BindingPatternTreeMatcher) checker); + } if (checker instanceof BlockTreeMatcher) { blockMatchers.add((BlockTreeMatcher) checker); } @@ -329,6 +362,9 @@ private void registerNodeTypes( if (checker instanceof EnhancedForLoopTreeMatcher) { enhancedForLoopMatchers.add((EnhancedForLoopTreeMatcher) checker); } + if (checker instanceof ExportsTreeMatcher) { + exportsMatchers.add((ExportsTreeMatcher) checker); + } if (checker instanceof ExpressionStatementTreeMatcher) { expressionStatementMatchers.add((ExpressionStatementTreeMatcher) checker); } @@ -374,12 +410,21 @@ private void registerNodeTypes( if (checker instanceof ModifiersTreeMatcher) { modifiersMatchers.add((ModifiersTreeMatcher) checker); } + if (checker instanceof ModuleTreeMatcher) { + moduleMatchers.add((ModuleTreeMatcher) checker); + } if (checker instanceof NewArrayTreeMatcher) { newArrayMatchers.add((NewArrayTreeMatcher) checker); } if (checker instanceof NewClassTreeMatcher) { newClassMatchers.add((NewClassTreeMatcher) checker); } + if (checker instanceof OpensTreeMatcher) { + opensMatchers.add((OpensTreeMatcher) checker); + } + if (checker instanceof PackageTreeMatcher) { + packageMatchers.add((PackageTreeMatcher) checker); + } if (checker instanceof ParameterizedTypeTreeMatcher) { parameterizedTypeMatchers.add((ParameterizedTypeTreeMatcher) checker); } @@ -389,9 +434,18 @@ private void registerNodeTypes( if (checker instanceof PrimitiveTypeTreeMatcher) { primitiveTypeMatchers.add((PrimitiveTypeTreeMatcher) checker); } + if (checker instanceof ProvidesTreeMatcher) { + providesMatchers.add((ProvidesTreeMatcher) checker); + } + if (checker instanceof RequiresTreeMatcher) { + requiresMatchers.add((RequiresTreeMatcher) checker); + } if (checker instanceof ReturnTreeMatcher) { returnMatchers.add((ReturnTreeMatcher) checker); } + if (checker instanceof SwitchExpressionTreeMatcher) { + switchExpressionMatchers.add((SwitchExpressionTreeMatcher) checker); + } if (checker instanceof SwitchTreeMatcher) { switchMatchers.add((SwitchTreeMatcher) checker); } @@ -416,6 +470,9 @@ private void registerNodeTypes( if (checker instanceof UnionTypeTreeMatcher) { unionTypeMatchers.add((UnionTypeTreeMatcher) checker); } + if (checker instanceof UsesTreeMatcher) { + usesMatchers.add((UsesTreeMatcher) checker); + } if (checker instanceof VariableTreeMatcher) { variableMatchers.add((VariableTreeMatcher) checker); } @@ -425,6 +482,9 @@ private void registerNodeTypes( if (checker instanceof WildcardTreeMatcher) { wildcardMatchers.add((WildcardTreeMatcher) checker); } + if (checker instanceof YieldTreeMatcher) { + yieldMatchers.add((YieldTreeMatcher) checker); + } } @FunctionalInterface @@ -513,6 +573,17 @@ public Void visitBinary(BinaryTree tree, VisitorState visitorState) { return super.visitBinary(tree, state); } + @Override + public Void visitBindingPattern(BindingPatternTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers( + bindingPatternMatchers, + tree, + BindingPatternTreeMatcher::matchBindingPattern, + visitorState); + return super.visitBindingPattern(tree, state); + } + @Override public Void visitBlock(BlockTree tree, VisitorState visitorState) { VisitorState state = @@ -620,6 +691,13 @@ public Void visitEnhancedForLoop(EnhancedForLoopTree tree, VisitorState visitorS return super.visitEnhancedForLoop(tree, state); } + @Override + public Void visitExports(ExportsTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(exportsMatchers, tree, ExportsTreeMatcher::matchExports, visitorState); + return super.visitExports(tree, state); + } + // Intentionally skip visitErroneous -- we don't analyze malformed expressions. @Override @@ -760,6 +838,14 @@ public Void visitModifiers(ModifiersTree tree, VisitorState visitorState) { return super.visitModifiers(tree, state); } + @Override + public Void visitModule(ModuleTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(moduleMatchers, tree, ModuleTreeMatcher::matchModule, visitorState); + + return super.visitModule(tree, state); + } + @Override public Void visitNewArray(NewArrayTree tree, VisitorState visitorState) { VisitorState state = @@ -774,6 +860,20 @@ public Void visitNewClass(NewClassTree tree, VisitorState visitorState) { return super.visitNewClass(tree, state); } + @Override + public Void visitOpens(OpensTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(opensMatchers, tree, OpensTreeMatcher::matchOpens, visitorState); + return super.visitOpens(tree, state); + } + + @Override + public Void visitPackage(PackageTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(packageMatchers, tree, PackageTreeMatcher::matchPackage, visitorState); + return super.visitPackage(tree, state); + } + // Intentionally skip visitOther. It seems to be used only for let expressions, which are // generated by javac to implement autoboxing. We are only interested in source-level constructs. @@ -810,6 +910,20 @@ public Void visitPrimitiveType(PrimitiveTypeTree tree, VisitorState visitorState return super.visitPrimitiveType(tree, state); } + @Override + public Void visitProvides(ProvidesTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(providesMatchers, tree, ProvidesTreeMatcher::matchProvides, visitorState); + return super.visitProvides(tree, state); + } + + @Override + public Void visitRequires(RequiresTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(requiresMatchers, tree, RequiresTreeMatcher::matchRequires, visitorState); + return super.visitRequires(tree, state); + } + @Override public Void visitReturn(ReturnTree tree, VisitorState visitorState) { VisitorState state = @@ -817,6 +931,17 @@ public Void visitReturn(ReturnTree tree, VisitorState visitorState) { return super.visitReturn(tree, state); } + @Override + public Void visitSwitchExpression(SwitchExpressionTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers( + switchExpressionMatchers, + tree, + SwitchExpressionTreeMatcher::matchSwitchExpression, + visitorState); + return super.visitSwitchExpression(tree, state); + } + @Override public Void visitSwitch(SwitchTree tree, VisitorState visitorState) { VisitorState state = @@ -878,6 +1003,13 @@ public Void visitUnionType(UnionTypeTree tree, VisitorState visitorState) { return super.visitUnionType(tree, state); } + @Override + public Void visitUses(UsesTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(usesMatchers, tree, UsesTreeMatcher::matchUses, visitorState); + return super.visitUses(tree, state); + } + @Override public Void visitVariable(VariableTree tree, VisitorState visitorState) { VisitorState state = @@ -900,6 +1032,13 @@ public Void visitWildcard(WildcardTree tree, VisitorState visitorState) { return super.visitWildcard(tree, state); } + @Override + public Void visitYield(YieldTree tree, VisitorState visitorState) { + VisitorState state = + processMatchers(yieldMatchers, tree, YieldTreeMatcher::matchYield, visitorState); + return super.visitYield(tree, state); + } + /** * Handles an exception thrown by an individual BugPattern. By default, wraps the exception in an * {@link ErrorProneError} and rethrows. May be overridden by subclasses, for example to log the