From d461d596f75b0e37d80d9b0aaab575a57e2751eb Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 7 Jun 2025 10:27:43 +0200 Subject: [PATCH] Java: check for immutable types in records (IDEA-372966) in "Assignment or return of field with mutable type" inspection GitOrigin-RevId: 96e782484fde1ad2229b19b30cf928847b04d4ba --- .../AssignmentOrReturnOfFieldWithMutableTypeInspection.java | 1 + .../AssignmentOrReturnOfFieldWithMutableType.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/java/java-impl/src/com/siyeh/ig/encapsulation/AssignmentOrReturnOfFieldWithMutableTypeInspection.java b/java/java-impl/src/com/siyeh/ig/encapsulation/AssignmentOrReturnOfFieldWithMutableTypeInspection.java index a8d889ddab15..579bae0d39a2 100644 --- a/java/java-impl/src/com/siyeh/ig/encapsulation/AssignmentOrReturnOfFieldWithMutableTypeInspection.java +++ b/java/java-impl/src/com/siyeh/ig/encapsulation/AssignmentOrReturnOfFieldWithMutableTypeInspection.java @@ -161,6 +161,7 @@ public final class AssignmentOrReturnOfFieldWithMutableTypeInspection extends Ba boolean reportAssignment = !ContainerUtil.or(recordClass.getConstructors(), c -> JavaPsiRecordUtil.isExplicitCanonicalConstructor(c)); for (PsiRecordComponent component : recordHeader.getRecordComponents()) { final PsiType type = component.getType(); + if (ClassUtils.isImmutable(type)) continue; final boolean mutable = type instanceof PsiArrayType || ContainerUtil.exists(MUTABLE_TYPES, typeName -> InheritanceUtil.isInheritor(type, typeName)); if (!mutable) continue; diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/encapsulation/assignment_or_return_of_field_with_mutable_type/AssignmentOrReturnOfFieldWithMutableType.java b/java/java-tests/testData/ig/com/siyeh/igtest/encapsulation/assignment_or_return_of_field_with_mutable_type/AssignmentOrReturnOfFieldWithMutableType.java index 92eca0c9a1a9..9965b8e7f46c 100644 --- a/java/java-tests/testData/ig/com/siyeh/igtest/encapsulation/assignment_or_return_of_field_with_mutable_type/AssignmentOrReturnOfFieldWithMutableType.java +++ b/java/java-tests/testData/ig/com/siyeh/igtest/encapsulation/assignment_or_return_of_field_with_mutable_type/AssignmentOrReturnOfFieldWithMutableType.java @@ -226,4 +226,6 @@ record Data4(Collection strings) { public Collection strings() { return strings; } +} +record Fooz(ImmutableList ints) { } \ No newline at end of file