Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle var in StringSplitter. #1129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,21 @@ public Boolean visitMemberSelect(MemberSelectTree tree, Void aVoid) {
return Optional.empty();
}
}

// Use of `var` causes the start position of the variable type tree node to be < 0
// Note that the .isImplicitlyTyped() method on JCVariableDecl returns the wrong answer after
// type attribution has occurred.
Tree varType = varTree.getType();
boolean isImplicitlyTyped = ((JCTree) varType).getStartPosition() < 0;
if (needsList[0]) {
fix.replace((varTree).getType(), "List<String>").addImport("java.util.List");
if (!isImplicitlyTyped) {
fix.replace(varType, "List<String>").addImport("java.util.List");
}
replaceWithSplitter(fix, tree, methodAndArgument, state, "splitToList", needsMutableList[0]);
} else {
fix.replace((varTree).getType(), "Iterable<String>");
if (!isImplicitlyTyped) {
fix.replace(varType, "Iterable<String>");
}
replaceWithSplitter(fix, tree, methodAndArgument, state, "split", needsMutableList[0]);
}
return Optional.of(fix.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.common.base.StandardSystemProperty;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
Expand Down Expand Up @@ -51,6 +54,29 @@ public void positive() {
.doTest(TestMode.TEXT_MATCH);
}

// Regression test for issue #1124
@Test
public void positive_localVarTypeInference() {
assumeTrue(isJdk10OrGreater());
testHelper
.addInputLines(
"Test.java",
"class Test {",
" void f() {",
" var lines = \"\".split(\":\");",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.google.common.base.Splitter;",
"class Test {",
" void f() {",
" var lines = Splitter.on(':').split(\"\");",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void positive_patternIsSymbol() {
testHelper
Expand Down Expand Up @@ -412,4 +438,14 @@ public void noSplitterOnClassPath() {
.setArgs("-cp", ":")
.doTest(TestMode.TEXT_MATCH);
}

private static boolean isJdk10OrGreater() {
try {
int majorVersion = Integer.parseInt(StandardSystemProperty.JAVA_VERSION.value());
return majorVersion >= 10;
} catch (NumberFormatException e) {
// OpenJDK versions <= 8 return something like "1.8.0"
return false;
}
}
}