nested switch: update for switch expressions (IDEA-203717)

This commit is contained in:
Anna.Kozlova
2018-12-20 16:22:46 +01:00
parent 5cdbcf8feb
commit 413d6003d3
5 changed files with 55 additions and 6 deletions
@@ -650,7 +650,7 @@ method.may.be.static.display.name=Method may be 'static'
class.initializer.may.be.static.display.name=Class initializer may be 'static'
class.initializer.option=Only warn when the class has one or more constructors
class.initializer.move.code.to.constructor.quickfix=Move initializer code to constructor
nested.switch.statement.display.name=Nested 'switch' statement
nested.switch.statement.display.name=Nested 'switch'
c.style.array.declaration.display.name=C-style array declaration
final.method.in.final.class.display.name='final' method in 'final' class
extends.annotation.display.name=Class extends annotation interface
@@ -867,7 +867,7 @@ conditional.expression.quickfix=Replace with 'if' statement
conditional.expression.semantics.quickfix=Replace with 'if' statement (may change semantics)
nested.conditional.expression.problem.descriptor=Nested conditional expression <code>#ref</code> #loc
long.literals.ending.with.lowercase.l.problem.descriptor='long' literal <code>#ref</code> ends with lowercase 'l' #loc
nested.switch.statement.problem.descriptor=Nested <code>#ref</code> statement #loc
nested.switch.statement.problem.descriptor=Nested <code>#ref</code> {0} #loc
chained.method.call.problem.descriptor=Chained method call <code>#ref()</code> #loc
nested.method.call.problem.descriptor=Nested method call <code>#ref()</code> #loc
octal.literal.problem.descriptor=Octal integer <code>#ref</code> #loc
@@ -15,6 +15,8 @@
*/
package com.siyeh.ig.controlflow;
import com.intellij.psi.PsiSwitchBlock;
import com.intellij.psi.PsiSwitchExpression;
import com.intellij.psi.PsiSwitchStatement;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
@@ -35,7 +37,7 @@ public class NestedSwitchStatementInspection extends BaseInspection {
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"nested.switch.statement.problem.descriptor");
"nested.switch.statement.problem.descriptor", infos[0] instanceof PsiSwitchStatement ? "statement" : "expression");
}
@Override
@@ -48,8 +50,16 @@ public class NestedSwitchStatementInspection extends BaseInspection {
@Override
public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) {
super.visitSwitchStatement(statement);
if (ControlFlowUtils.isNestedElement(statement, PsiSwitchStatement.class)) {
registerStatementError(statement);
if (ControlFlowUtils.isNestedElement(statement, PsiSwitchBlock.class)) {
registerStatementError(statement, statement);
}
}
@Override
public void visitSwitchExpression(PsiSwitchExpression expression) {
super.visitSwitchExpression(expression);
if (ControlFlowUtils.isNestedElement(expression, PsiSwitchBlock.class)) {
registerError(expression.getFirstChild(), expression);
}
}
}
@@ -1,6 +1,6 @@
<html>
<body>
Reports nested <b>switch</b> statements. Nested <b>switch</b> statements
Reports nested <b>switch</b> statements/expressions. Nested <b>switch</b>-es
may result in extremely confusing code.
<!-- tooltip end -->
<p>
@@ -0,0 +1,11 @@
class MyTest {
int y(int i) {
return switch (i) {
case 0 -> 0;
default -> <warning descr="Nested 'switch' expression">switch</warning> (i) {
case 100 -> 0;
default -> i;
};
};
}
}
@@ -0,0 +1,28 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.controlflow;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NestedSwitchInspectionTest extends LightInspectionTestCase {
public void testNestedSwitch() {
doTest();
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return LightCodeInsightFixtureTestCase.JAVA_12;
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new NestedSwitchStatementInspection();
}
}