From 14b88366d4a8a870c14ad3475c179a122bbcae77 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Fri, 9 Nov 2018 15:22:53 -0600 Subject: [PATCH] Make FallThrough resilient to missing source information. Update the FallThrough check to gracefully handle missing source code information, which occurs when using certain Lombok annotations. This is similar to the existing workaround added to the ParameterName bug pattern. Fixes #613. --- .../com/google/errorprone/bugpatterns/FallThrough.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java b/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java index 843756e4267..f3925c55250 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java @@ -32,6 +32,8 @@ import com.sun.source.tree.BlockTree; import com.sun.source.tree.SwitchTree; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.util.Position; + import java.util.regex.Pattern; /** @author cushon@google.com (Liam Miller-Cushon) */ @@ -63,10 +65,16 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) { // reported an error if that statement wasn't reachable, and the answer is // independent of any preceding statements. boolean completes = Reachability.canCompleteNormally(getLast(caseTree.stats)); + int caseEndPositionIndex = caseEndPosition(state, caseTree); + int nextStartPositionIndex = next.getStartPosition(); + // best effort work-around for https://github.com/google/error-prone/issues/613 + if (caseEndPositionIndex == Position.NOPOS || nextStartPositionIndex == Position.NOPOS) { + continue; + } String comments = state .getSourceCode() - .subSequence(caseEndPosition(state, caseTree), next.getStartPosition()) + .subSequence(caseEndPositionIndex, nextStartPositionIndex) .toString() .trim(); if (completes && !FALL_THROUGH_PATTERN.matcher(comments).find()) {