Skip to content

Commit

Permalink
Fix a crash in InvalidLink on /// markdown doc comments
Browse files Browse the repository at this point in the history
`[foo]` is getting 'desugared' to a link node for `{@link foo}` that doesn't have a start position, for now this change avoids a crash and skip emitting a fix for that case.

PiperOrigin-RevId: 661643507
  • Loading branch information
cushon authored and Error Prone Team committed Aug 11, 2024
1 parent ac6597f commit 1dacc8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static SuggestedFix replace(DocTree docTree, String replacement, VisitorState st
int startPos = getStartPosition(docTree, state);
int endPos =
(int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree);
if (endPos == Position.NOPOS) {
if (startPos == Position.NOPOS || endPos == Position.NOPOS) {
return SuggestedFix.emptyFix();
}
return SuggestedFix.replace(startPos, endPos, replacement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package com.google.errorprone.bugpatterns.javadoc;

import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -181,4 +183,19 @@ public void emptyLinkTest() {
"}")
.doTest();
}

@Test
public void markdown() {
assumeTrue(RuntimeVersion.isAtLeast22());
helper
.addSourceLines(
"Test.java", //
"public class Test {",
" /// Hello [bar]",
" // BUG: Diagnostic contains: `bar` is a parameter",
" static void foo(int bar) {}",
"}")
.setArgs("--release", "22")
.doTest();
}
}

0 comments on commit 1dacc8d

Please sign in to comment.