Skip to content

Commit

Permalink
replaceIncludingComments: skip synthetic variable declarations in rec…
Browse files Browse the repository at this point in the history
…ords.

This isn't perfect because it fails to find the canonical constructor as a member, but it'll prevent howling great bugs.

PiperOrigin-RevId: 714993354
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 13, 2025
1 parent 318ed1a commit ce0f344
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasImplicitType;
import static com.google.errorprone.util.ASTHelpers.isRecord;
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 @@ -1641,7 +1642,11 @@ public static SuggestedFix replaceIncludingComments(
ClassTree classTree = (ClassTree) parent;
int startTokenization;
for (Tree member : classTree.getMembers()) {
if (member instanceof MethodTree && ASTHelpers.isGeneratedConstructor((MethodTree) member)) {
if (member instanceof MethodTree methodTree
&& ASTHelpers.isGeneratedConstructor(methodTree)) {
continue;
}
if (member instanceof VariableTree && isRecord(getSymbol(member))) {
continue;
}
if (member.equals(tree)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,6 @@ public long getId() {
}
}
""")
// TODO(kak): we shouldn't delete the closing parenthesis and open curly bracket!
.addOutputLines(
"Client.java",
"""
Expand All @@ -1015,8 +1014,7 @@ public long getId() {
import com.google.errorprone.annotations.InlineMe;
public final class Client {
public record SomeRecord(long id
}
public record SomeRecord(long id) {}
}
""")
.doTest(TestMode.TEXT_MATCH);
Expand Down

0 comments on commit ce0f344

Please sign in to comment.