mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-235296 Refactor ensureCodeBlock: inspections and quick-fixes updated
PointlessBooleanExpressionInspection, SimplifyBooleanExpressionFix, DeleteReturnFix, SurroundWithTryCatchFix, ToMutableCollectionConverter GitOrigin-RevId: 6bc897c5cb64e04cd4d6528e45aa2b7a39186901
This commit is contained in:
committed by
intellij-monorepo-bot
parent
593aa0bac0
commit
cb1731985b
+2
-6
@@ -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<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(returnValue);
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
+8
-5
@@ -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;
|
||||
|
||||
|
||||
+6
-5
@@ -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<PsiExpression> 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;
|
||||
}
|
||||
|
||||
+6
-6
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user