From 65fc6979542c0f1315565e59aca7397df50c5101 Mon Sep 17 00:00:00 2001 From: Error Prone Team Date: Sat, 31 Dec 2022 06:48:48 -0800 Subject: [PATCH] UngroupedOverloads: ignore generated constructors Fixes #3404. Fixes #3632 FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/3632 from PicnicSupermarket:bugfix/issue-3404 897aa796de924a1c1b3c12b898598b09900d5342 PiperOrigin-RevId: 498732782 --- .../bugpatterns/UngroupedOverloads.java | 4 +++- .../bugpatterns/UngroupedOverloadsTest.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UngroupedOverloads.java b/core/src/main/java/com/google/errorprone/bugpatterns/UngroupedOverloads.java index c29df14641a2..7ef3334b7a63 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UngroupedOverloads.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UngroupedOverloads.java @@ -96,7 +96,9 @@ public Description matchClass(ClassTree classTree, VisitorState state) { Tree member = classTree.getMembers().get(i); if (member instanceof MethodTree) { MethodTree methodTree = (MethodTree) member; - methods.put(OverloadKey.create(methodTree), MemberWithIndex.create(i, methodTree)); + if (!ASTHelpers.isGeneratedConstructor(methodTree)) { + methods.put(OverloadKey.create(methodTree), MemberWithIndex.create(i, methodTree)); + } } } ImmutableList descriptions = diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UngroupedOverloadsTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UngroupedOverloadsTest.java index 69c33408a9fc..f2fb2305beba 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UngroupedOverloadsTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UngroupedOverloadsTest.java @@ -16,10 +16,13 @@ package com.google.errorprone.bugpatterns; +import static org.junit.Assume.assumeTrue; + import com.google.common.collect.ImmutableList; import com.google.errorprone.BugCheckerRefactoringTestHelper; import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; import com.google.errorprone.CompilationTestHelper; +import com.google.errorprone.util.RuntimeVersion; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -287,4 +290,22 @@ public void describingConstructors() { "}") .doTest(); } + + @Test + public void recordConstructor() { + assumeTrue(RuntimeVersion.isAtLeast16()); + + compilationHelper + .addSourceLines( + "Test.java", + "import com.google.common.collect.ImmutableSet;", + "import java.util.Set;", + "", + "record MyRecord(ImmutableSet strings) {", + " MyRecord(Set strings) {", + " this(strings == null ? ImmutableSet.of() : ImmutableSet.copyOf(strings));", + " }", + "}") + .doTest(); + } }