Skip to content

Commit 1dcc15d

Browse files
cushonError Prone Team
authored andcommitted
Fix a crash in DeprecatedVariable on synthetic record trees
`@Deprecated` annotations on record components are propagated to the getter methods, where they do have an effect. PiperOrigin-RevId: 891873165
1 parent 351abc2 commit 1dcc15d

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.errorprone.matchers.Description;
2929
import com.google.errorprone.util.ASTHelpers;
3030
import com.sun.source.tree.VariableTree;
31+
import com.sun.tools.javac.code.Flags;
3132
import com.sun.tools.javac.code.Symbol;
3233
import java.util.Optional;
3334

@@ -48,6 +49,10 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
4849
return NO_MATCH;
4950
}
5051
}
52+
if ((sym.flags() & Flags.MANDATED) == Flags.MANDATED) {
53+
// Don't match on synthetic variable declarations for record components
54+
return NO_MATCH;
55+
}
5156
Description.Builder description = buildDescription(tree);
5257
Optional.ofNullable(
5358
getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Deprecated"))

core/src/test/java/com/google/errorprone/bugpatterns/DeprecatedVariableTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,22 @@ void f() {
7878
""")
7979
.doTest();
8080
}
81+
82+
@Test
83+
public void recordComponent() {
84+
testHelper
85+
.addInputLines(
86+
"Test.java",
87+
"""
88+
import java.util.Objects;
89+
90+
public record Test(@Deprecated int x) {
91+
public Test {
92+
Objects.requireNonNull(x);
93+
}
94+
}
95+
""")
96+
.expectUnchanged()
97+
.doTest();
98+
}
8199
}

0 commit comments

Comments
 (0)