From ff379ff0e6410f15632fbd2d21c6cd5e865cd189 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Sat, 10 Aug 2024 11:25:27 -0700 Subject: [PATCH] Fix a crash in InvalidLink on `///` markdown doc comments `[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 --- .../google/errorprone/util/RuntimeVersion.java | 5 +++++ .../errorprone/bugpatterns/javadoc/Utils.java | 2 +- .../bugpatterns/javadoc/InvalidLinkTest.java | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java index 0785489c6450..28e95215a2a6 100644 --- a/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java +++ b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java @@ -84,6 +84,11 @@ public static boolean isAtLeast22() { return FEATURE >= 22; } + /** Returns true if the current runtime is JDK 23 or newer. */ + public static boolean isAtLeast23() { + return FEATURE >= 23; + } + /** * Returns the latest {@code --release} version. * diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java index 1a81cf644787..0219c862da30 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/Utils.java @@ -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); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/InvalidLinkTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/InvalidLinkTest.java index 03cca7a05d5c..bad5c2775fda 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/InvalidLinkTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/InvalidLinkTest.java @@ -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; @@ -181,4 +183,19 @@ public void emptyLinkTest() { "}") .doTest(); } + + @Test + public void markdown() { + assumeTrue(RuntimeVersion.isAtLeast23()); + 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(); + } }