RedundantCollectionOperationHandler (PsiMethodCallExpression call) { PsiExpression qualifier1 = call.getMethodExpression().getQualifierExpression(); if (qualifier1 == null) return null; CallMatcher wantedMethod = anyOf(COLLECTION_REMOVE, MAP_REMOVE); PsiElement parent = PsiUtil.skipParenthesizedExprUp(call.getParent()); if (parent instanceof PsiExpression && BoolUtils.isNegation((PsiExpression)parent)) { wantedMethod = SET_ADD; parent = PsiUtil.skipParenthesizedExprUp(parent.getParent()); } if (parent instanceof PsiPolyadicExpression && ((PsiPolyadicExpression)parent).getOperationTokenType().equals(JavaTokenType.ANDAND) && PsiTreeUtil.isAncestor(ArrayUtil.getLastElement(((PsiPolyadicExpression)parent).getOperands()), call, false)) { parent = PsiUtil.skipParenthesizedExprUp(parent.getParent()); } PsiIfStatement ifStatement = tryCast(parent, PsiIfStatement.class); if (ifStatement == null) return null; if (ifStatement.getElseBranch() != null) return null; PsiExpressionStatement thenBody = tryCast(ControlFlowUtils.stripBraces(ifStatement.getThenBranch()), PsiExpressionStatement.class); if (thenBody == null) return null; PsiMethodCallExpression thenCall = tryCast(thenBody.getExpression(), PsiMethodCallExpression.class); if (!wantedMethod.test(thenCall)) return null; PsiExpression qualifier2 = thenCall.getMethodExpression().getQualifierExpression(); if (qualifier2 == null || !PsiEquivalenceUtil.areElementsEquivalent(qualifier1, qualifier2)) return null; if (!PsiEquivalenceUtil.areElementsEquivalent(call.getArgumentList(), thenCall.getArgumentList())) return null; return new ContainsBeforeAddRemoveHandler(call.getMethodExpression().getReferenceName()); }