PsiImplUtil#getSwitchLabel reused (IDEA-CR-39949)

This commit is contained in:
Tagir Valeev
2018-11-16 16:21:02 +07:00
parent ca16031b1c
commit 5d9fac39d0
5 changed files with 17 additions and 27 deletions
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
@@ -308,13 +309,13 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool
Set<PsiSwitchBlock> coveredSwitches = new HashSet<>();
Set<PsiExpression> trueLabels = StreamEx.of(trueSet).select(ConditionalGotoInstruction.class)
.map(ConditionalGotoInstruction::getPsiAnchor).select(PsiExpression.class)
.filter(e -> SwitchUtils.getLabelStatementForLabel(e) != null).toSet();
.filter(e -> PsiImplUtil.getSwitchLabel(e) != null).toSet();
Set<PsiExpression> falseLabels = StreamEx.of(falseSet).select(ConditionalGotoInstruction.class)
.map(ConditionalGotoInstruction::getPsiAnchor).select(PsiExpression.class)
.filter(e -> SwitchUtils.getLabelStatementForLabel(e) != null).toSet();
.filter(e -> PsiImplUtil.getSwitchLabel(e) != null).toSet();
for (PsiExpression label : trueLabels) {
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(SwitchUtils.getLabelStatementForLabel(label));
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(PsiImplUtil.getSwitchLabel(label));
PsiSwitchBlock statement = labelStatement.getEnclosingSwitchBlock();
if (statement == null) continue;
if (!StreamEx.iterate(labelStatement, Objects::nonNull, l -> PsiTreeUtil.getPrevSiblingOfType(l, PsiSwitchLabelStatementBase.class))
@@ -327,7 +328,7 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool
createUnwrapSwitchLabelFix());
}
for (PsiExpression label : falseLabels) {
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(SwitchUtils.getLabelStatementForLabel(label));
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(PsiImplUtil.getSwitchLabel(label));
if (!coveredSwitches.contains(labelStatement.getEnclosingSwitchBlock())) {
holder.registerProblem(label, InspectionsBundle.message("dataflow.message.unreachable.switch.label"),
new DeleteSwitchLabelFix(label));
@@ -730,7 +731,7 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool
}
}
else if (psiAnchor != null &&
(!(psiAnchor instanceof PsiExpression) || SwitchUtils.getLabelStatementForLabel((PsiExpression)psiAnchor) == null) &&
(!(psiAnchor instanceof PsiExpression) || PsiImplUtil.getSwitchLabel((PsiExpression)psiAnchor) == null) &&
!isFlagCheck(psiAnchor)) {
boolean evaluatesToTrue = trueSet.contains(instruction);
final PsiElement parent = psiAnchor.getParent();
@@ -5,13 +5,13 @@ import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.SwitchUtils;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -27,7 +27,7 @@ public class DeleteSwitchLabelFix implements LocalQuickFix {
public DeleteSwitchLabelFix(@NotNull PsiExpression label) {
myName = label.getText();
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(SwitchUtils.getLabelStatementForLabel(label));
PsiSwitchLabelStatementBase labelStatement = Objects.requireNonNull(PsiImplUtil.getSwitchLabel(label));
PsiExpressionList values = labelStatement.getCaseValues();
boolean multiple = values != null && values.getExpressionCount() > 1;
myBranch = !multiple && shouldRemoveBranch(labelStatement);
@@ -63,7 +63,7 @@ public class DeleteSwitchLabelFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiExpression expression = ObjectUtils.tryCast(descriptor.getStartElement(), PsiExpression.class);
if (expression == null) return;
PsiSwitchLabelStatementBase label = SwitchUtils.getLabelStatementForLabel(expression);
PsiSwitchLabelStatementBase label = PsiImplUtil.getSwitchLabel(expression);
if (label == null) return;
PsiExpressionList values = label.getCaseValues();
if (values != null && values.getExpressionCount() == 1) {
@@ -7,10 +7,10 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.dataFlow.fix.DeleteSwitchLabelFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.SwitchUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -28,7 +28,7 @@ public class UnwrapSwitchLabelFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiExpression label = ObjectUtils.tryCast(descriptor.getStartElement(), PsiExpression.class);
if (label == null) return;
PsiSwitchLabelStatementBase labelStatement = SwitchUtils.getLabelStatementForLabel(label);
PsiSwitchLabelStatementBase labelStatement = PsiImplUtil.getSwitchLabel(label);
if (labelStatement == null) return;
PsiSwitchStatement statement = labelStatement.getEnclosingSwitchStatement();
if (statement == null) return;
@@ -632,6 +632,12 @@ public class PsiImplUtil {
}
}
/**
* Returns enclosing label statement for given label expression
*
* @param expression switch label expression
* @return enclosing label statement or null if given expression is not a label statement
*/
@Nullable
public static PsiSwitchLabelStatementBase getSwitchLabel(@NotNull PsiExpression expression) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
@@ -23,8 +23,6 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -241,21 +239,6 @@ public class SwitchUtils {
}
}
/**
* Returns enclosing label statement for given label expression
*
* @param expression switch label expression
* @return enclosing label statement or null if given expression is not a label statement
*/
@Contract("null -> null")
@Nullable
public static PsiSwitchLabelStatementBase getLabelStatementForLabel(PsiExpression expression) {
if (expression == null) return null;
PsiElement parent = expression.getParent();
if (!(parent instanceof PsiExpressionList)) return null;
return ObjectUtils.tryCast(parent.getParent(), PsiSwitchLabelStatementBase.class);
}
private static boolean checkForLabel(String name, PsiElement ancestor) {
final LabelSearchVisitor visitor = new LabelSearchVisitor(name);
ancestor.accept(visitor);