From 384493143b8f16c8c41f9233f1d091f8780796c4 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Wed, 16 Jul 2025 17:12:27 +0200 Subject: [PATCH] [java-inspections] IDEA-374865 ClassCanBeRecord: fix edge case with method call in anonymous class constructor In response to IJ-CR-167896 GitOrigin-RevId: 3915e8b287cdeaeaf1fdfc436661110f84d76434 --- .../ConstructorBodyProcessor.java | 12 ++++++++++- .../beforeDelegating_11.java | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/classCanBeRecord/flexibleConstructorBodies/beforeDelegating_11.java diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConstructorBodyProcessor.java b/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConstructorBodyProcessor.java index 9a979fff725f..bdb8ae500d3f 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConstructorBodyProcessor.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConstructorBodyProcessor.java @@ -260,7 +260,17 @@ final class ConstructorBodyProcessor { @Override public void visitClass(PsiClass aClass) { - // Empty on purpose. + if (aClass instanceof PsiAnonymousClass anonymousClass) { + PsiExpressionList arguments = anonymousClass.getArgumentList(); + if (arguments != null) { + for (PsiExpression expression : arguments.getExpressions()) { + if (hasReferenceToContainingClass(containingClass, expression)) { + markInvalid(); + return; + } + } + } + } } @Override diff --git a/java/java-tests/testData/inspection/classCanBeRecord/flexibleConstructorBodies/beforeDelegating_11.java b/java/java-tests/testData/inspection/classCanBeRecord/flexibleConstructorBodies/beforeDelegating_11.java new file mode 100644 index 000000000000..f83d6b09a597 --- /dev/null +++ b/java/java-tests/testData/inspection/classCanBeRecord/flexibleConstructorBodies/beforeDelegating_11.java @@ -0,0 +1,20 @@ +// "Convert to record class" "false" +class Person { + final String name; + final int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + Person(Person person) { + new Other(hashCode()) {}; + this(person.name, person.age); + } +} + +class Other { + Other(int x) { + } +} \ No newline at end of file