Skip to content

Commit 679c4ac

Browse files
cushonError Prone Team
authored andcommitted
Update end position handling
After openjdk/jdk@43af7b5, javac sets the end position to the start position instead of NOPOS for nodes that don't have an end position. PiperOrigin-RevId: 836370643
1 parent dc1279e commit 679c4ac

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

check_api/src/main/java/com/google/errorprone/VisitorState.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
22+
import static com.google.errorprone.util.ASTHelpers.hasExplicitSource;
2223

2324
import com.google.common.annotations.VisibleForTesting;
2425
import com.google.common.collect.ImmutableList;
@@ -557,7 +558,7 @@ public Type arrayTypeForType(Type baseType) {
557558
int start = getStartPosition(tree);
558559
int end = getEndPosition(tree);
559560
CharSequence source = getSourceCode();
560-
if (end == -1) {
561+
if (!hasExplicitSource(tree, this)) {
561562
return null;
562563
}
563564
checkArgument(start >= 0, "invalid start position (%s) for: %s", start, tree);

check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ private static SuggestedFix renameClass(ClassTree tree, String replacement, Visi
768768
int endPos =
769769
tree.getMembers().stream()
770770
.map(state::getEndPosition)
771-
.filter(p -> p != NOPOS)
771+
.filter(p -> p != NOPOS && p != basePos)
772772
.findFirst()
773773
.orElse(state.getEndPosition(tree));
774774
List<ErrorProneToken> tokens = state.getOffsetTokens(basePos, endPos);

check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,9 @@ public static boolean hasImplicitType(VariableTree tree, VisitorState state) {
21302130

21312131
/** Returns whether the given tree has an explicit source code representation. */
21322132
public static boolean hasExplicitSource(Tree tree, VisitorState state) {
2133-
return getStartPosition(tree) != Position.NOPOS && state.getEndPosition(tree) != Position.NOPOS;
2133+
int pos = getStartPosition(tree);
2134+
int endPos = state.getEndPosition(tree);
2135+
return pos != Position.NOPOS && endPos != Position.NOPOS && pos != endPos;
21342136
}
21352137

21362138
/** Returns {@code true} if this symbol was declared in Kotlin source. */

core/src/main/java/com/google/errorprone/bugpatterns/AbstractMustBeClosedChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static com.google.errorprone.util.ASTHelpers.getSymbol;
3333
import static com.google.errorprone.util.ASTHelpers.getType;
3434
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
35+
import static com.google.errorprone.util.ASTHelpers.hasExplicitSource;
3536
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
3637
import static com.google.errorprone.util.ASTHelpers.isConsideredFinal;
3738
import static com.google.errorprone.util.ASTHelpers.isInStaticInitializer;
@@ -70,7 +71,6 @@
7071
import com.sun.tools.javac.code.Symbol.MethodSymbol;
7172
import com.sun.tools.javac.code.Symbol.VarSymbol;
7273
import com.sun.tools.javac.code.Type;
73-
import com.sun.tools.javac.util.Position;
7474
import java.util.Objects;
7575
import java.util.Optional;
7676
import java.util.stream.Stream;
@@ -507,7 +507,7 @@ private static Optional<Change> wrapTryFinallyAroundVariableScope(
507507
}
508508
Tree declTree = decl.getType();
509509
String declType =
510-
state.getEndPosition(declTree) == Position.NOPOS ? "var" : state.getSourceForNode(declTree);
510+
!hasExplicitSource(declTree, state) ? "var" : state.getSourceForNode(declTree);
511511

512512
return Change.builder(
513513
SuggestedFix.builder()

0 commit comments

Comments
 (0)