From 19d8016c05cb06de1836679d6ca38330165d65bd Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 2 Jan 2025 08:34:18 -0800 Subject: [PATCH] UnnecessaryBoxedVariable: don't flag parameters to the canonical constructor on records. Seems safer to avoid this case, I think: we'd be changing the record itself, not just a method. PiperOrigin-RevId: 711433332 --- .../bugpatterns/UnnecessaryBoxedVariable.java | 3 ++- .../UnnecessaryBoxedVariableTest.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariable.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariable.java index eec50a07517..068e7a84a85 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariable.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariable.java @@ -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 { diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariableTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariableTest.java index 74c5212ed8e..89032326634 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariableTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedVariableTest.java @@ -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(); + } }