Skip to content

Commit

Permalink
Apply suggestion, format
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 29, 2023
1 parent 74f9ac5 commit 3741524
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static com.google.errorprone.util.ASTHelpers.getModifiers;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasNoExplicitType;
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
import static com.sun.source.tree.Tree.Kind.ASSIGNMENT;
import static com.sun.source.tree.Tree.Kind.CONDITIONAL_EXPRESSION;
import static com.sun.source.tree.Tree.Kind.NEW_ARRAY;
Expand Down Expand Up @@ -1118,7 +1118,7 @@ private static Tree suppressibleNode(TreePath path, VisitorState state) {
&& ((ClassTree) tree).getSimpleName().length() != 0)
// Lambda parameters can't be suppressed unless they have Type decls
|| (tree instanceof VariableTree
&& !hasNoExplicitType((VariableTree) tree, state)))
&& !hasImplicitType((VariableTree) tree, state)))
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2646,17 +2646,16 @@ private void scan(Type from, Type to) {
}

/** Returns {@code true} if this is a `var` or a lambda parameter that has no explicit type. */
public static boolean hasNoExplicitType(VariableTree tree, VisitorState state) {
public static boolean hasImplicitType(VariableTree tree, VisitorState state) {
/*
* Note that the .isImplicitlyTyped() method on JCVariableDecl returns the wrong answer after
* type attribution has occurred.
* For lambda expression parameters without an explicit type, both
* `JCVariableDecl#declaredUsingVar()` and `#isImplicitlyTyped()` may be false. So instead we
* check whether the variable's type is explicitly represented in the source code.
*/
return !hasExplicitSource(tree.getType(), state);
}

/**
* Returns {@code true} if the given tree has an explicit source code representation.
*/
/** Returns {@code true} if the given tree has an explicit source code representation. */
public static boolean hasExplicitSource(Tree tree, VisitorState state) {
return getStartPosition(tree) != Position.NOPOS && state.getEndPosition(tree) != Position.NOPOS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public Void visitVariable(VariableTree node, Void unused) {
if (!sym.equals(ASTHelpers.getSymbol(node))) {
return null;
}
if (ASTHelpers.hasNoExplicitType(node, state)) {
if (ASTHelpers.hasImplicitType(node, state)) {
// ignore synthetic tree nodes for `var`
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public Void visitIdentifier(IdentifierTree tree, Void unused) {
}
SuggestedFix.Builder fix =
SuggestedFix.builder().replace(newClassTree.getIdentifier(), "StringBuilder");
if (!ASTHelpers.hasNoExplicitType(varTree, state)) {
if (!ASTHelpers.hasImplicitType(varTree, state)) {
// If the variable is declared with `var`, there's no declaration type to change
fix = fix.replace(varTree.getType(), "StringBuilder");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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.hasImplicitType;
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 @@ -211,7 +211,7 @@ public Boolean visitMemberSelect(MemberSelectTree tree, Void unused) {
}

Tree varType = varTree.getType();
boolean isImplicitlyTyped = hasNoExplicitType(varTree, state); // Is it a use of `var`?
boolean isImplicitlyTyped = hasImplicitType(varTree, state); // Is it a use of `var`?
if (needsList[0]) {
if (!isImplicitlyTyped) {
fix.replace(varType, "List<String>").addImport("java.util.List");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasNoExplicitType;
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
import static com.google.errorprone.util.ASTHelpers.isConsideredFinal;
import static com.google.errorprone.util.ASTHelpers.isSameType;
import static com.google.errorprone.util.ASTHelpers.streamReceivers;
Expand Down Expand Up @@ -74,7 +74,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
if (!symbol.getKind().equals(LOCAL_VARIABLE)
|| !isConsideredFinal(symbol)
|| initializer == null
|| hasNoExplicitType(tree, state)) {
|| hasImplicitType(tree, state)) {
return NO_MATCH;
}
// Foo foo = (Foo) bar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasNoExplicitType;
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
import static javax.lang.model.element.ElementKind.PARAMETER;
import static javax.lang.model.type.TypeKind.TYPEVAR;

Expand Down Expand Up @@ -143,7 +143,7 @@ public Description matchBinary(BinaryTree tree, VisitorState state) {
if (param == null) {
return NO_MATCH; // hopefully impossible: A parameter must come from the same compilation unit
}
if (hasNoExplicitType(param, state)) {
if (hasImplicitType(param, state)) {
return NO_MATCH;
}
SuggestedFix fix = fixByAddingNullableAnnotationToType(state, param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasExplicitSource;
import static com.google.errorprone.util.ASTHelpers.hasNoExplicitType;
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
import static com.sun.source.tree.Tree.Kind.METHOD;
import static javax.lang.model.element.ElementKind.LOCAL_VARIABLE;

Expand Down Expand Up @@ -147,7 +147,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return NO_MATCH;
}

if (hasNoExplicitType(tree, state)) {
if (hasImplicitType(tree, state)) {
/*
* In the case of `var`, a declaration-annotation @Nullable would be valid. But a type-use
* @Nullable would not be. But more importantly, we expect that tools will infer the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void negativeGenericLambdaParameterNoType() {
aggressiveCompilationHelper
.addSourceLines(
"Test.java",
"import org.jspecify.annotations.Nullable;",
"import org.jspecify.annotations.Nullable;",
"interface Test {",
" void consume(Iterable<@Nullable Void> it);",
"",
Expand Down

0 comments on commit 3741524

Please sign in to comment.