From d7e586657a31c61a3c8037a5697c26c2d6fb0071 Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 8 Jan 2020 01:57:34 -0800 Subject: [PATCH] MissingSummary: don't fail in the case that javac fails to provide an endpos for @see. Fixes external #1444 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=288656035 --- .../bugpatterns/javadoc/MissingSummary.java | 24 ++++++++++++------- .../errorprone/bugpatterns/javadoc/Utils.java | 4 ++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java index 6e7cb4ef7b8..74ae0c8efb9 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java @@ -149,16 +149,22 @@ private Description generateReturnFix( private Description generateSeeFix(DocTreePath docTreePath, SeeTree seeTree, VisitorState state) { int pos = ((DCDocComment) docTreePath.getDocComment()).comment.getSourcePos(0); + // javac fails to provide an endpos for @see sometimes; don't emit a fix in that case. + SuggestedFix replacement = Utils.replace(seeTree, "", state); SuggestedFix fix = - SuggestedFix.builder() - .merge(Utils.replace(seeTree, "", state)) - .replace( - pos, - pos, - String.format( - "See {@link %s}.\n", - seeTree.getReference().stream().map(Object::toString).collect(joining(" ")))) - .build(); + replacement.isEmpty() + ? replacement + : SuggestedFix.builder() + .merge(replacement) + .replace( + pos, + pos, + String.format( + "See {@link %s}.\n", + seeTree.getReference().stream() + .map(Object::toString) + .collect(joining(" ")))) + .build(); return buildDescription(diagnosticPosition(docTreePath, state)) .setMessage(String.format(CONSIDER_USING_MESSAGE, "see")) .addFix(fix) 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 457c041f7e0..4465bed8ca0 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 @@ -32,6 +32,7 @@ import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; +import com.sun.tools.javac.util.Position; import java.util.Optional; import javax.annotation.Nullable; @@ -65,6 +66,9 @@ 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) { + return SuggestedFix.builder().build(); + } return SuggestedFix.replace(startPos, endPos, replacement); }