Skip to content

Commit

Permalink
Work around b/118437729
Browse files Browse the repository at this point in the history
RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218727951
  • Loading branch information
cushon committed Nov 2, 2018
1 parent 264fe64 commit 8ff28af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/main/java/com/google/errorprone/bugpatterns/Unused.java
Expand Up @@ -92,6 +92,7 @@
import com.sun.tools.javac.tree.JCTree.JCAssign;
import com.sun.tools.javac.tree.JCTree.JCAssignOp;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.util.Position;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -763,7 +764,12 @@ public Void visitMethod(MethodTree tree, Void unused) {

private void removeByIndex(List<? extends Tree> trees) {
if (trees.size() == 1) {
fix.delete(getOnlyElement(trees));
Tree tree = getOnlyElement(trees);
if (((JCTree) tree).getStartPosition() == -1 || state.getEndPosition(tree) == -1) {
// TODO(b/118437729): handle bogus source positions in enum declarations
return;
}
fix.delete(tree);
return;
}
int startPos;
Expand All @@ -778,6 +784,10 @@ private void removeByIndex(List<? extends Tree> trees) {
if (index == methodSymbol.params().size() - 1 && methodSymbol.isVarArgs()) {
endPos = state.getEndPosition(getLast(trees));
}
if (startPos == Position.NOPOS || endPos == Position.NOPOS) {
// TODO(b/118437729): handle bogus source positions in enum declarations
return;
}
fix.replace(startPos, endPos, "");
}
}.scan(state.getPath().getCompilationUnit(), null);
Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.ErrorProneFlags;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -876,4 +877,56 @@ public void fixPrivateMethod_varArgs() {
"}")
.doTest();
}

@Ignore("b/118437729")
@Test
public void enumField() {
refactoringHelper
.addInputLines(
"Test.java", //
"enum Test {",
" ONE(\"1\", 1) {};",
" private String a;",
" private Test(String a, int x) {",
" this.a = a;",
" }",
" String a() {",
" return a;",
" }",
"}")
.addOutputLines(
"Test.java", //
"enum Test {",
" ONE(\"1\") {};",
" private String a;",
" private Test(String a) {",
" this.a = a;",
" }",
" String a() {",
" return a;",
" }",
"}")
.doTest();
}

@Ignore("b/118437729")
@Test
public void onlyEnumField() {
refactoringHelper
.addInputLines(
"Test.java", //
"enum Test {",
" ONE(1) {};",
" private Test(int x) {",
" }",
"}")
.addOutputLines(
"Test.java", //
"enum Test {",
" ONE() {};",
" private Test() {",
" }",
"}")
.doTest();
}
}

0 comments on commit 8ff28af

Please sign in to comment.