Skip to content

Commit 4c0f01b

Browse files
cushonError Prone Team
authored andcommitted
Report InvalidLink diagnostics for links that are so invalid they crash javac
PiperOrigin-RevId: 829336029
1 parent b5690c9 commit 4c0f01b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/javadoc/InvalidLink.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,24 @@ public Void visitLink(LinkTree linkTree, Void unused) {
150150
// Install a deferred diagnostic handler before calling DocTrees.getElement(DocTreePath)
151151

152152
Log.DeferredDiagnosticHandler deferredDiagnosticHandler = deferredDiagnosticHandler(log);
153+
boolean crashed = false;
153154
try {
154155
element =
155156
JavacTrees.instance(state.context)
156157
.getElement(new DocTreePath(getCurrentPath(), linkTree.getReference()));
157158
} catch (NullPointerException | AssertionError e) {
158-
// TODO: cushon - remove if https://bugs.openjdk.org/browse/JDK-8371248 is fixed
159+
crashed = true;
159160
} finally {
160161
log.popDiagnosticHandler(deferredDiagnosticHandler);
161162
}
163+
if (crashed) {
164+
// If the @link crashed javac, report a finding.
165+
// TODO: cushon - remove if https://bugs.openjdk.org/browse/JDK-8371248 is fixed
166+
state.reportMatch(
167+
buildDescription(diagnosticPosition(getCurrentPath(), state))
168+
.addFix(replace(linkTree, String.format("{@code %s}", reference), state))
169+
.build());
170+
}
162171
// Don't warn about fully qualified types; they won't always be known at compile-time.
163172
if (element != null || reference.contains(".")) {
164173
return super.visitLink(linkTree, null);

core/src/test/java/com/google/errorprone/bugpatterns/javadoc/InvalidLinkTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,20 @@ static void foo(int bar) {}
227227
.setArgs("--release", "22")
228228
.doTest();
229229
}
230+
231+
// https://bugs.openjdk.org/browse/JDK-8371248
232+
@Test
233+
public void crash() {
234+
helper
235+
.addSourceLines(
236+
"Test.java",
237+
"""
238+
public class Test {
239+
// BUG: Diagnostic contains:
240+
/** {@link double.NAN} */
241+
static void foo() {}
242+
}
243+
""")
244+
.doTest();
245+
}
230246
}

0 commit comments

Comments
 (0)