void (@NotNull PsiElement element) { // just delete it altogether PsiElement parent = element.getParent(); if (parent instanceof PsiExpressionStatement) { if (parent.getParent() instanceof PsiCodeBlock || isForLoopUpdate(parent)) { parent.delete(); } else { // replace with empty statement (to handle with 'if (..) i=0;' ) // if element used in expression, subexpression will do String replacement = parent.getParent() instanceof PsiSwitchLabeledRuleStatement ? "{}" : ";"; PsiElement result = JavaPsiFacade.getElementFactory(parent.getProject()).createStatementFromText(replacement, null); parent.replace(result); } } else if (parent instanceof PsiExpressionList list && parent.getParent() instanceof PsiExpressionListStatement) { PsiExpression[] expressions = list.getExpressions(); if (expressions.length == 2) { PsiExpression other = expressions[0] == element ? expressions[1] : expressions[0]; replaceElementWithExpression(other, JavaPsiFacade.getElementFactory(parent.getProject()), parent); } else { element.delete(); } } else if (element.getParent() instanceof PsiLambdaExpression) { element.replace(JavaPsiFacade.getElementFactory(parent.getProject()).createCodeBlock()); } else { element.delete(); } }