Skip to content

Commit

Permalink
Work around apparent javadoc parsing limitation with empty comments
Browse files Browse the repository at this point in the history
Fixes google#1981

PiperOrigin-RevId: 350677284
  • Loading branch information
cushon authored and stevie400 committed Jan 15, 2021
1 parent b378657 commit 3445562
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.tree.CompilationUnitTree;
Expand Down Expand Up @@ -87,6 +88,11 @@ static int getEndPosition(DocTree docTree, VisitorState state) {
static DiagnosticPosition diagnosticPosition(DocTreePath path, VisitorState state) {
int startPosition = getStartPosition(path.getLeaf(), state);
Tree tree = path.getTreePath().getLeaf();
if (startPosition == Position.NOPOS) {
// javac doesn't seem to store positions for e.g. trivial empty javadoc like `/** */`
// see: https://github.com/google/error-prone/issues/1981
startPosition = ASTHelpers.getStartPosition(tree);
}
return getDiagnosticPosition(startPosition, tree);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ public void negative() {
.doTest();
}

@Test
public void negativeEmpty() {
helper
.addSourceLines(
"Test.java", //
"interface Test {",
" /** */",
" int test();",
"}")
.doTest();
}

@Test
public void negativeAnnotations() {
helper
Expand Down Expand Up @@ -222,4 +210,17 @@ public void emptyReturn() {
"}")
.doTest();
}

@Test
public void emptyComment() {
helper
.addSourceLines(
"Test.java", //
"package test;",
"/** */",
"// BUG: Diagnostic contains: summary line is required",
"public class Test {",
"}")
.doTest();
}
}

0 comments on commit 3445562

Please sign in to comment.