Skip to content

Commit

Permalink
Fix a SIOOBE in Unused.
Browse files Browse the repository at this point in the history
This occurs from confusing source positions when "int a, b;" is desugared into "int a; int b;".

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218883432
  • Loading branch information
graememorgan authored and cushon committed Nov 2, 2018
1 parent 055cde4 commit 33f676c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -846,10 +846,14 @@ private static SuggestedFix replaceWithComments(
.sorted(Comparator.<Comment>comparingInt(c -> c.getSourcePos(0)).reversed())
.collect(toImmutableList());
int startPos = ((JCTree) tree).getStartPosition() - startTokenization;
// This can happen for desugared expressions like `int a, b;`.
if (startPos < 0) {
return SuggestedFix.builder().build();
}
// Delete backwards for comments which are not separated from our target by a blank line.
for (Comment comment : comments) {
String stringBetweenComments =
source.substring(comment.getSourcePos(comment.getText().length() - 1), startPos);
int endOfCommentPos = comment.getSourcePos(comment.getText().length() - 1);
String stringBetweenComments = source.substring(endOfCommentPos, startPos);
if (stringBetweenComments.chars().filter(c -> c == '\n').count() > 1) {
break;
}
Expand Down
Expand Up @@ -612,6 +612,21 @@ public void removal_javadocAndSingleLines() {
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void unusedWithComment_interspersedComments() {
helper
.addSourceLines(
"UnusedWithComment.java",
"package unusedvars;",
"public class UnusedWithComment {",
" private static final String foo = null, // foo",
" // BUG: Diagnostic contains: Unused",
" bar = null;",
" public static String foo() { return foo; }",
"}")
.doTest();
}

@Test
public void usedInLambda() {
helper
Expand Down

0 comments on commit 33f676c

Please sign in to comment.