Skip to content

Commit

Permalink
Further consolidate on ASTHelpers.hasNoExplicitType.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 493419134
  • Loading branch information
cpovirk authored and Error Prone Team committed Dec 6, 2022
1 parent fb1d0b9 commit 6f4c1fa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,9 @@ public static boolean hasNoExplicitType(VariableTree tree, VisitorState state) {
* we see a start position, we then also look for an end position, which *is* absent for
* lambda parameters, even under javac8. Possibly we could get by looking *only* for the end
* position, but I'm keeping both checks now that I have something that appears to work.
*
* Note that the .isImplicitlyTyped() method on JCVariableDecl returns the wrong answer after
* type attribution has occurred.
*/
return getStartPosition(tree.getType()) == -1 || state.getEndPosition(tree.getType()) == -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasNoExplicitType;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
import static com.google.errorprone.util.Regexes.convertRegexToLiteral;
import static java.lang.String.format;
Expand Down Expand Up @@ -209,11 +210,8 @@ public Boolean visitMemberSelect(MemberSelectTree tree, Void unused) {
}
}

// 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 = getStartPosition(varType) < 0;
boolean isImplicitlyTyped = hasNoExplicitType(varTree, state); // Is it a use of `var`?
if (needsList[0]) {
if (!isImplicitlyTyped) {
fix.replace(varType, "List<String>").addImport("java.util.List");
Expand Down

0 comments on commit 6f4c1fa

Please sign in to comment.