From cb1731985b9708c7a33ab06b7db2d4ec01ec2e89 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 16 Mar 2020 18:36:25 +0700 Subject: [PATCH] IDEA-235296 Refactor ensureCodeBlock: inspections and quick-fixes updated PointlessBooleanExpressionInspection, SimplifyBooleanExpressionFix, DeleteReturnFix, SurroundWithTryCatchFix, ToMutableCollectionConverter GitOrigin-RevId: 6bc897c5cb64e04cd4d6528e45aa2b7a39186901 --- .../daemon/impl/quickfix/DeleteReturnFix.java | 8 ++------ .../impl/quickfix/SimplifyBooleanExpressionFix.java | 7 ++++--- .../impl/quickfix/SurroundWithTryCatchFix.java | 13 ++++++++----- .../PointlessBooleanExpressionInspection.java | 11 ++++++----- .../collections/ImmutableCollectionModelUtils.java | 12 ++++++------ 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteReturnFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteReturnFix.java index e7a73bce90b5..0dc506e418c0 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteReturnFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DeleteReturnFix.java @@ -7,11 +7,7 @@ import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; -import com.intellij.refactoring.util.RefactoringUtil; -import com.siyeh.ig.psiutils.CommentTracker; -import com.siyeh.ig.psiutils.ControlFlowUtils; -import com.siyeh.ig.psiutils.SideEffectChecker; -import com.siyeh.ig.psiutils.StatementExtractor; +import com.siyeh.ig.psiutils.*; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -63,7 +59,7 @@ public class DeleteReturnFix extends LocalQuickFixAndIntentionActionOnPsiElement if (returnValue == null) return; CommentTracker ct = new CommentTracker(); if (myHasSideEffects) { - returnValue = Objects.requireNonNull(RefactoringUtil.ensureCodeBlock(returnValue)); + returnValue = Objects.requireNonNull(CodeBlockSurrounder.forExpression(returnValue)).surround().getExpression(); returnStatement = (PsiReturnStatement)returnValue.getParent(); } List sideEffects = SideEffectChecker.extractSideEffectExpressions(returnValue); diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 526a8b8e1e41..d2c179e6f12b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -73,7 +73,7 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { } private boolean canExtractSideEffect(PsiExpression subExpression) { - if (ControlFlowUtils.canExtractStatement(subExpression)) return true; + if (CodeBlockSurrounder.canSurround(subExpression)) return true; if (!mySubExpressionValue) { PsiElement parent = PsiUtil.skipParenthesizedExprUp(subExpression.getParent()); if (parent instanceof PsiWhileStatement || parent instanceof PsiForStatement) return true; @@ -176,7 +176,7 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { simplifyExpression(expression); } - public PsiExpression ensureCodeBlock(@NotNull Project project, PsiExpression subExpression) { + private PsiExpression ensureCodeBlock(@NotNull Project project, PsiExpression subExpression) { if (!mySubExpressionValue) { // Prevent extracting while condition to internal 'if' PsiElement parent = PsiUtil.skipParenthesizedExprUp(subExpression.getParent()); @@ -200,7 +200,8 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { } } } - return RefactoringUtil.ensureCodeBlock(subExpression); + CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(subExpression); + return surrounder == null ? null : surrounder.surround().getExpression(); } @Nullable diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java index 2938c0a7c13f..7867ffd9ec92 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java @@ -13,7 +13,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.refactoring.util.RefactoringUtil; import com.intellij.util.IncorrectOperationException; -import com.siyeh.ig.psiutils.ControlFlowUtils; +import com.siyeh.ig.psiutils.CodeBlockSurrounder; import com.siyeh.ig.psiutils.ExpressionUtils; import org.jetbrains.annotations.NotNull; @@ -27,7 +27,7 @@ public class SurroundWithTryCatchFix implements IntentionAction { element instanceof PsiResourceVariable || (element instanceof PsiExpression && !(element instanceof PsiMethodReferenceExpression) && - ControlFlowUtils.canExtractStatement(ExpressionUtils.getTopLevelExpression((PsiExpression)element)))) { + CodeBlockSurrounder.canSurround(ExpressionUtils.getTopLevelExpression((PsiExpression)element)))) { myElement = element; } } @@ -62,10 +62,13 @@ public class SurroundWithTryCatchFix implements IntentionAction { editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(0, 0)); if (myElement instanceof PsiExpression) { - myElement = RefactoringUtil.ensureCodeBlock(ExpressionUtils.getTopLevelExpression((PsiExpression)myElement)); + CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(ExpressionUtils.getTopLevelExpression((PsiExpression)myElement)); + if (surrounder == null) return; + myElement = surrounder.surround().getAnchor(); + } else { + myElement = RefactoringUtil.getParentStatement(myElement, false); + if (myElement == null) return; } - myElement = RefactoringUtil.getParentStatement(myElement, false); - if (myElement == null) return; TextRange range = null; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java index 759d3ac03e85..6c399bdad1ad 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java @@ -370,10 +370,11 @@ public class PointlessBooleanExpressionInspection extends BaseInspection { String simplifiedExpression = buildSimplifiedExpression(expression, new StringBuilder(), tracker).toString(); boolean isConstant = simplifiedExpression.equals("true") || simplifiedExpression.equals("false"); if (isConstant && myHasSideEffect) { - expression = RefactoringUtil.ensureCodeBlock(expression); - if (expression == null) return; - PsiStatement anchor = PsiTreeUtil.getParentOfType(expression, PsiStatement.class); - if (anchor == null) return; + CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(expression); + if (surrounder == null) return; + CodeBlockSurrounder.SurroundResult result = surrounder.surround(); + expression = result.getExpression(); + PsiStatement anchor = result.getAnchor(); List sideEffects = extractSideEffects(expression); for (PsiExpression sideEffect : sideEffects) { tracker.markUnchanged(sideEffect); @@ -498,7 +499,7 @@ public class PointlessBooleanExpressionInspection extends BaseInspection { } if (containsConstant) { if (sideEffectMayBeRemoved && reducedToConstant) { - return ControlFlowUtils.canExtractStatement(expression) + return CodeBlockSurrounder.canSurround(expression) ? BooleanExpressionKind.USELESS_WITH_SIDE_EFFECTS : BooleanExpressionKind.UNKNOWN; } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java index 556276690693..cb702ae72447 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java @@ -13,7 +13,6 @@ import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer; -import com.intellij.refactoring.util.RefactoringUtil; import com.intellij.util.ObjectUtils; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.callMatcher.CallMapper; @@ -39,7 +38,7 @@ class ImmutableCollectionModelUtils { static ImmutableCollectionModel createModel(@NotNull PsiMethodCallExpression call) { CollectionType type = CollectionType.create(call); if (type == null) return null; - if (!ControlFlowUtils.canExtractStatement(call)) return null; + if (!CodeBlockSurrounder.canSurround(call)) return null; String assignedVariable = getAssignedVariable(call); PsiExpression[] args = call.getArgumentList().getExpressions(); PsiMethod method = call.resolveMethod(); @@ -170,10 +169,11 @@ class ImmutableCollectionModelUtils { } private void replaceWithMutable(@NotNull ImmutableCollectionModel model) { - PsiMethodCallExpression call = RefactoringUtil.ensureCodeBlock(model.myCall); - if (call == null) return; - PsiStatement statement = ObjectUtils.tryCast(RefactoringUtil.getParentStatement(call, false), PsiStatement.class); - if (statement == null) return; + CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(model.myCall); + if (surrounder == null) return; + CodeBlockSurrounder.SurroundResult result = surrounder.surround(); + PsiMethodCallExpression call = (PsiMethodCallExpression)result.getExpression(); + PsiStatement statement = result.getAnchor(); model.myCall = call; String assignedVariable = model.myAssignedVariable;