Skip to content

Commit

Permalink
Handle record component annotations in RemoveUnusedImports
Browse files Browse the repository at this point in the history
Annotations are moved to the corresponding synthetic element before Error Prone runs, so explicitly scan annotations on record members.

PiperOrigin-RevId: 718436538
  • Loading branch information
cushon authored and Error Prone Team committed Jan 22, 2025
1 parent 68a9a13 commit 0cabca8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.getEnclosedElements;
import static com.google.errorprone.util.ASTHelpers.getSymbol;

import com.google.common.base.Joiner;
Expand All @@ -34,6 +35,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.ReferenceTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
Expand Down Expand Up @@ -142,6 +144,17 @@ public Void visitIdentifier(IdentifierTree tree, SymbolSink sink) {
return null;
}

@Override
public Void visitClass(ClassTree node, SymbolSink symbolSink) {
if (node.getKind().equals(Tree.Kind.RECORD)) {
getEnclosedElements(getSymbol(node)).stream()
.flatMap(e -> e.getAnnotationMirrors().stream())
.map(a -> (Symbol) a.getAnnotationType().asElement())
.forEach(symbolSink::accept);
}
return super.visitClass(node, symbolSink);
}

@Override
public Void scan(Tree tree, SymbolSink sink) {
if (!sink.keepScanning()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,10 @@ public record Test(int z, @One int x, int y) {}
"""
package p;
// TODO: b/390690031 - this should be preserved
// import a.One;
import a.One;
public record Test(int z, @One int x, int y) {}
""")
.allowBreakingChanges() // TODO: b/390690031 - this not be breaking
.doTest();
}
}

0 comments on commit 0cabca8

Please sign in to comment.