[java-inspection] IDEA-379322 Pattern conversion is suggested when the record variable is used

GitOrigin-RevId: d1d378f0264d23227afbadcb11ec4f2f8c2dcf49
This commit is contained in:
Mikhail Pyltsin
2025-09-16 11:58:39 +00:00
committed by intellij-monorepo-bot
parent 2e21c8f3ce
commit c3826e657d
2 changed files with 14 additions and 4 deletions
@@ -66,16 +66,13 @@ public final class DeconstructionCanBeUsedInspection extends AbstractBaseJavaLoc
continue;
}
PsiRecordComponent component = getComponent(reference);
if (component == null) continue;
if (component == null) return Collections.emptyList();
if (!used.add(component) && !shouldFindAll) continue;
PsiElementFactory factory = PsiElementFactory.getInstance(reference.getProject());
PsiExpression call = factory.createExpressionFromText(reference.getText() + "." + component.getName() + "()", reference);
if (SideEffectChecker.mayHaveSideEffects(call)) return Collections.emptyList();
int index = ArrayUtil.indexOf(components, component);
result.get(index).add((PsiReferenceExpression)PsiUtil.skipParenthesizedExprUp(reference.getParent()));
if (!shouldFindAll && used.size() == components.length) {
return result;
}
}
return used.size() == components.length ? result : Collections.emptyList();
}
@@ -0,0 +1,13 @@
// "Replace with record pattern" "false"
class X {
record R(String oldText, String newText) {
}
void patternBug(Object obj) {
if (obj instanceof R<caret> r) {
System.out.println(r.oldText());
System.out.println(r.newText());
System.out.println(r);
}
}
}