Skip to content

Commit

Permalink
Enable EmptyTopLevelDeclaration.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 443668189
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 22, 2022
1 parent edd51fd commit 5bd42a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@

package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import java.util.ArrayList;
import java.util.List;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(summary = "Empty top-level type declaration", severity = WARNING)
public class EmptyTopLevelDeclaration extends BugChecker implements CompilationUnitTreeMatcher {
@BugPattern(summary = "Empty top-level type declarations should be omitted", severity = ERROR)
public final class EmptyTopLevelDeclaration extends BugChecker
implements CompilationUnitTreeMatcher {

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
List<Tree> toDelete = new ArrayList<>();
for (Tree member : tree.getTypeDecls()) {
if (member.getKind() == Tree.Kind.EMPTY_STATEMENT) {
toDelete.add(member);
}
}
ImmutableList<Tree> toDelete =
tree.getTypeDecls().stream()
.filter(m -> m.getKind() == Tree.Kind.EMPTY_STATEMENT)
.collect(toImmutableList());
if (toDelete.isEmpty()) {
return Description.NO_MATCH;
return NO_MATCH;
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
for (Tree member : toDelete) {
fixBuilder.delete(member);
}
toDelete.forEach(fixBuilder::delete);
return describeMatch(toDelete.get(0), fixBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ public static ScannerSupplier errorChecks() {
DurationGetTemporalUnit.class,
DurationTemporalUnit.class,
DurationToLongTimeUnit.class,
EmptyTopLevelDeclaration.class,
EqualsHashCode.class,
EqualsNaN.class,
EqualsNull.class,
Expand Down Expand Up @@ -1017,7 +1018,6 @@ public static ScannerSupplier errorChecks() {
DepAnn.class,
DifferentNameButSame.class,
EmptyIfStatement.class,
EmptyTopLevelDeclaration.class,
EqualsBrokenForNull.class,
EqualsMissingNullable.class,
ExpectedExceptionChecker.class,
Expand Down

0 comments on commit 5bd42a2

Please sign in to comment.