mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java-inspections] IDEA-315243 Java Inspection "Minimum 'switch' branches" deletes switch with side effects
GitOrigin-RevId: ceebbaf5c0d8affa9469856fb16c878727d9a6a1
This commit is contained in:
committed by
intellij-monorepo-bot
parent
415445c4bd
commit
50818e9d76
@@ -129,10 +129,11 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti
|
||||
final String expressionText;
|
||||
final Project project = switchStatement.getProject();
|
||||
int totalCases = allBranches.stream().mapToInt(br -> br.getCaseElements().size()).sum();
|
||||
List<PsiExpression> sideEffectExpressions = SideEffectChecker.extractSideEffectExpressions(switchExpression);
|
||||
if (totalCases > 0) {
|
||||
commentTracker.markUnchanged(switchExpression);
|
||||
}
|
||||
if (totalCases > 1 && RemoveUnusedVariableUtil.checkSideEffects(switchExpression, null, new ArrayList<>())) {
|
||||
if (totalCases > 1 && !sideEffectExpressions.isEmpty()) {
|
||||
hadSideEffects = true;
|
||||
|
||||
final String variableName = new VariableNameGenerator(switchExpression, VariableKind.LOCAL_VARIABLE)
|
||||
@@ -141,7 +142,7 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti
|
||||
declarationString = switchExpressionType.getCanonicalText() + ' ' + variableName + " = " + switchExpression.getText() + ';';
|
||||
}
|
||||
else {
|
||||
hadSideEffects = false;
|
||||
hadSideEffects = totalCases == 0 && !sideEffectExpressions.isEmpty();
|
||||
declarationString = null;
|
||||
expressionText = ParenthesesUtils.getPrecedence(switchExpression) > ParenthesesUtils.EQUALITY_PRECEDENCE
|
||||
? '(' + switchExpression.getText() + ')'
|
||||
@@ -191,8 +192,15 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti
|
||||
}
|
||||
JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
if (hadSideEffects) {
|
||||
final PsiStatement declarationStatement = factory.createStatementFromText(declarationString, switchStatement);
|
||||
javaCodeStyleManager.shortenClassReferences(parent.addBefore(declarationStatement, switchStatement));
|
||||
if (declarationString == null) {
|
||||
PsiStatement[] statements = StatementExtractor.generateStatements(sideEffectExpressions, switchExpression);
|
||||
for (PsiStatement statement : statements) {
|
||||
javaCodeStyleManager.shortenClassReferences(parent.addBefore(statement, switchStatement));
|
||||
}
|
||||
} else {
|
||||
final PsiStatement declarationStatement = factory.createStatementFromText(declarationString, switchStatement);
|
||||
javaCodeStyleManager.shortenClassReferences(parent.addBefore(declarationStatement, switchStatement));
|
||||
}
|
||||
}
|
||||
final PsiStatement ifStatement = factory.createStatementFromText(ifStatementText, switchStatement);
|
||||
if (unwrapDefault) {
|
||||
@@ -422,7 +430,7 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti
|
||||
PsiPatternVariable variable = typeTestPattern.getPatternVariable();
|
||||
if (variable == null) return null;
|
||||
PsiElement context = PsiTreeUtil.getParentOfType(variable, PsiSwitchStatement.class);
|
||||
boolean isUsedPatternVariable = context != null && VariableAccessUtils.variableIsUsed(variable, context);
|
||||
boolean isUsedPatternVariable = VariableAccessUtils.variableIsUsed(variable, context);
|
||||
PsiIdentifier identifier = variable.getNameIdentifier();
|
||||
String variableName = isUsedPatternVariable ? commentTracker.textWithComments(identifier) : commentTracker.commentsBefore(identifier);
|
||||
return expressionText + " instanceof " + typeText + " " + variableName;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Unwrap 'switch'" "true-preview"
|
||||
class X {
|
||||
int[] someArray;
|
||||
|
||||
native boolean someCondition(int i);
|
||||
|
||||
void test(int i) {
|
||||
do {
|
||||
--i;
|
||||
} while (someCondition(i));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Unwrap 'switch'" "true-preview"
|
||||
class X {
|
||||
int[] someArray;
|
||||
|
||||
native boolean someCondition(int i);
|
||||
|
||||
void test(int i) {
|
||||
do {
|
||||
switch<caret> (someArray[--i]) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} while (someCondition(i));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user