void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if(!(element.getParent() instanceof PsiLoopStatement loop)) return; PsiStatement[] statements = ControlFlowUtils.unwrapBlock(loop.getBody()); PsiIfStatement ifStatement = tryCast(ArrayUtil.getLastElement(statements), PsiIfStatement.class); if (ifStatement == null) return; PsiExpression condition = ifStatement.getCondition(); if (condition == null) return; String replacement; CommentTracker ct = new CommentTracker(); if (loop instanceof PsiForeachStatement) { ForEachCollectionTraversal traversal = ForEachCollectionTraversal.fromLoop((PsiForeachStatement)loop); if (traversal == null || statements.length != 1) return; replacement = generateRemoveIf(traversal, ct, condition, traversal.getParameter().getName()); } else { IteratorDeclaration declaration = IteratorDeclaration.fromLoop(loop); if (declaration == null) return; switch (statements.length) { case 1 -> { PsiElement ref = declaration.findOnlyIteratorRef(condition); if (ref == null) return; PsiElement call = ref.getParent().getParent(); if (!declaration.isIteratorMethodCall(call, "next")) return; PsiType type = ((PsiExpression)call).getType(); JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); SuggestedNameInfo info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, type); if (info.names.length == 0) { info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, "value", null, type); } String paramName = javaCodeStyleManager.suggestUniqueVariableName(info, condition, true).names[0]; ct.replace(call, JavaPsiFacade.getElementFactory(project).createIdentifier(paramName)); replacement = generateRemoveIf(declaration, ct, condition, paramName); } case 2 -> { PsiVariable variable = declaration.getNextElementVariable(statements[0]); if (variable == null) return; replacement = generateRemoveIf(declaration, ct, condition, variable.getName()); } default -> { return; } } ct.delete(declaration.getIterator()); } PsiElement result = ct.replaceAndRestoreComments(loop, replacement); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); CodeStyleManager.getInstance(project).reformat(result); }