From f0c3c1eb1b576ee9bc44f1f21c9379e7a02dd745 Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 30 Oct 2024 09:53:23 -0700 Subject: [PATCH] Add a test confirming that ArgumentSelectionDefectChecker works on record construction, but _not_ record deconstruction. PiperOrigin-RevId: 691455946 --- .../ArgumentSelectionDefectCheckerTest.java | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/argumentselectiondefects/ArgumentSelectionDefectCheckerTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/argumentselectiondefects/ArgumentSelectionDefectCheckerTest.java index d505aa55c8e..6a650688d5c 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/argumentselectiondefects/ArgumentSelectionDefectCheckerTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/argumentselectiondefects/ArgumentSelectionDefectCheckerTest.java @@ -15,6 +15,8 @@ */ package com.google.errorprone.bugpatterns.argumentselectiondefects; +import static com.google.common.truth.TruthJUnit.assume; + import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; import com.google.errorprone.BugPattern.SeverityLevel; @@ -38,6 +40,10 @@ @RunWith(JUnit4.class) public class ArgumentSelectionDefectCheckerTest { + private final CompilationTestHelper testHelper = + CompilationTestHelper.newInstance( + ArgumentSelectionDefectWithStringEquality.class, getClass()); + /** * A {@link BugChecker} which runs the ArgumentSelectionDefectChecker checker using string * equality for edit distance @@ -56,7 +62,7 @@ public ArgumentSelectionDefectWithStringEquality() { @Test public void argumentSelectionDefectChecker_findsSwap_withSwappedMatchingPair() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -75,7 +81,7 @@ void test(Object first, Object second) { @Test public void argumentSelectionDefectChecker_findsSwap_withSwappedMatchingPairWithMethod() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -96,7 +102,7 @@ void test(Object first) { @Test public void argumentSelectionDefectChecker_findsSwap_withOneNullArgument() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -115,7 +121,7 @@ void test(Object second) { @Test public void argumentSelectionDefectChecker_rejectsSwap_withNoAssignableAlternatives() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -132,7 +138,7 @@ void test(Integer first, String second) { @Test public void argumentSelectionDefectChecker_commentsOnlyOnSwappedPair_withThreeArguments() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -360,7 +366,7 @@ public void parameterNamesAvailable_returnsTree_onMethodNotInCompilationUnit() { @Test public void description() { - CompilationTestHelper.newInstance(ArgumentSelectionDefectWithStringEquality.class, getClass()) + testHelper .addSourceLines( "Test.java", """ @@ -374,6 +380,48 @@ void test(Object first, Object second) { target(second, first); } } +""") + .doTest(); + } + + @Test + public void records() { + assume().that(Runtime.version().feature()).isAtLeast(16); + + testHelper + .addSourceLines( + "Test.java", + """ +class Test { + Foo test(String first, String second) { + // BUG: Diagnostic contains: may have been swapped + return new Foo(second, first); + } +} + +record Foo(String first, String second) {} +""") + .doTest(); + } + + @Test + public void recordDeconstruction() { + assume().that(Runtime.version().feature()).isAtLeast(21); + + testHelper + .addSourceLines( + "Test.java", + """ +class Test { + void test(Foo foo) { + switch (foo) { + // TODO(user): We should report a finding here! + case Foo(String second, String first) -> {} + } + } +} + +record Foo(String first, String second) {} """) .doTest(); }