From 897aa796de924a1c1b3c12b898598b09900d5342 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 29 Dec 2022 14:28:12 +0100 Subject: [PATCH] UngroupedOverloads: ignore generated constructors Fixes #3404. --- .../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 c29df14641a..7ef3334b7a6 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 69c33408a9f..f2fb2305beb 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(); + } }