Skip to content

Commit

Permalink
UnnecessaryBoxedVariable: don't flag parameters to the canonical cons…
Browse files Browse the repository at this point in the history
…tructor on records.

Seems safer to avoid this case, I think: we'd be changing the record itself, not just a method.

PiperOrigin-RevId: 711433332
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 2, 2025
1 parent 114a058 commit 19d8016
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ private static boolean isBoxableType(Type type, VisitorState state) {

private static boolean canChangeMethodSignature(VisitorState state, MethodSymbol methodSymbol) {
return !ASTHelpers.methodCanBeOverridden(methodSymbol)
&& ASTHelpers.findSuperMethods(methodSymbol, state.getTypes()).isEmpty();
&& ASTHelpers.findSuperMethods(methodSymbol, state.getTypes()).isEmpty()
&& !ASTHelpers.isRecord(methodSymbol);
}

private static class FindBoxedUsagesScanner extends TreePathScanner<Void, Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,21 @@ static Foo create(Integer foo, Boolean bar) {
.setArgs(ImmutableList.of("-processor", AutoValueProcessor.class.getName()))
.doTest();
}

@Test
public void recordsIgnored() {
compilationTestHelper
.addSourceLines(
"Foo.java",
"""
import static com.google.common.base.Preconditions.checkNotNull;
record Foo(Integer foo, boolean bar) {
Foo {
checkNotNull(foo);
}
}
""")
.doTest();
}
}

0 comments on commit 19d8016

Please sign in to comment.