Skip to content

Commit ea312c9

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Don't make UnnecessarilyFullyQualified suggestions in package-infos
Fixes #1652 PiperOrigin-RevId: 350823261
1 parent 4275784 commit ea312c9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/UnnecessarilyFullyQualified.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
7171
t -> getSymbol(tree) != null && !getGeneratedBy(getSymbol(tree), state).isEmpty())) {
7272
return NO_MATCH;
7373
}
74+
if (isPackageInfo(tree)) {
75+
return NO_MATCH;
76+
}
7477
Table<Name, TypeSymbol, List<TreePath>> table = HashBasedTable.create();
7578
Set<Name> identifiersSeen = new HashSet<>();
7679
new SuppressibleTreePathScanner<Void, Void>() {
@@ -183,4 +186,13 @@ public Void visitIdentifier(IdentifierTree identifierTree, Void aVoid) {
183186
}
184187
return NO_MATCH;
185188
}
189+
190+
private boolean isPackageInfo(CompilationUnitTree tree) {
191+
String name = tree.getSourceFile().getName();
192+
int idx = name.lastIndexOf('/');
193+
if (idx != -1) {
194+
name = name.substring(idx + 1);
195+
}
196+
return name.equals("package-info.java");
197+
}
186198
}

core/src/test/java/com/google/errorprone/bugpatterns/UnnecessarilyFullyQualifiedTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.errorprone.bugpatterns;
1717

1818
import com.google.errorprone.BugCheckerRefactoringTestHelper;
19+
import com.google.errorprone.CompilationTestHelper;
1920
import org.junit.Test;
2021
import org.junit.runner.RunWith;
2122
import org.junit.runners.JUnit4;
@@ -130,4 +131,18 @@ public void builder() {
130131
"}")
131132
.doTest();
132133
}
134+
135+
@Test
136+
public void packageInfo() {
137+
CompilationTestHelper.newInstance(UnnecessarilyFullyQualified.class, getClass())
138+
.addSourceLines(
139+
"a/A.java", //
140+
"package a;",
141+
"public @interface A {}")
142+
.addSourceLines(
143+
"b/package-info.java", //
144+
"@a.A",
145+
"package b;")
146+
.doTest();
147+
}
133148
}

0 commit comments

Comments
 (0)