void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element.getParent() instanceof PsiForStatement statement)) return; PsiStatement body = statement.getBody(); if (body == null) return; PsiStatement[] statements = ControlFlowUtils.unwrapBlock(body); PsiMethodCallExpression call = getLastMethodCall(statements); if (!LIST_SET.test(call)) return; PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(call.getMethodExpression()); if (qualifier == null) return; CountingLoop countingLoop = CountingLoop.from(statement); if (countingLoop == null) return; IndexedContainer container = IndexedContainer.fromLengthExpression(countingLoop.getBound()); if (container == null) return; List<PsiMethodCallExpression> getCalls = collectGetCalls(body, container, countingLoop.getCounter()); String paramName = generateParameterName(body, getCalls); getCalls.forEach(getCall -> { new CommentTracker().replaceAndRestoreComments(getCall, paramName); }); if (statements.length == 2) { inlineVariable(getVariableToInline(statements[0], body), body); } else if (statements.length == 3) { inlineVariable(getVariableToInline(statements[0], body), body); inlineVariable(getVariableToInline(statements[1], body), body); } CommentTracker ct = new CommentTracker(); PsiExpression replacementExpression = call.getArgumentList().getExpressions()[1]; PsiElement returnStatement = new CommentTracker().replaceAndRestoreComments(call.getParent(), "return " + ct.textWithComments(replacementExpression) + ";"); String codeBlockText = body instanceof PsiBlockStatement ? ct.text(body) : "{ " + ct.text(returnStatement) + " }"; String replaceAllParameter = paramName + " -> " + codeBlockText; String text = ct.text(qualifier) + ".replaceAll(" + replaceAllParameter + ");"; PsiElement result = ct.replaceAndRestoreComments(statement, text); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); simplifyToExpressionLambda(result); CodeStyleManager.getInstance(project).reformat(result); }