diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 8c147e371e80..f8b944b82cc0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -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 #ref #loc long.literals.ending.with.lowercase.l.problem.descriptor='long' literal #ref ends with lowercase 'l' #loc -nested.switch.statement.problem.descriptor=Nested #ref statement #loc +nested.switch.statement.problem.descriptor=Nested #ref {0} #loc chained.method.call.problem.descriptor=Chained method call #ref() #loc nested.method.call.problem.descriptor=Nested method call #ref() #loc octal.literal.problem.descriptor=Octal integer #ref #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java index 3174e0b3b5a4..0325750ab3a5 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/NestedSwitchStatementInspection.java @@ -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); } } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/NestedSwitchStatement.html b/plugins/InspectionGadgets/src/inspectionDescriptions/NestedSwitchStatement.html index b6b0597ecac0..4cee5c2807a9 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/NestedSwitchStatement.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/NestedSwitchStatement.html @@ -1,6 +1,6 @@ -Reports nested switch statements. Nested switch statements +Reports nested switch statements/expressions. Nested switch-es may result in extremely confusing code.

diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/nested_switch_statement/NestedSwitch.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/nested_switch_statement/NestedSwitch.java new file mode 100644 index 000000000000..1f8552ceda74 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/nested_switch_statement/NestedSwitch.java @@ -0,0 +1,11 @@ +class MyTest { + int y(int i) { + return switch (i) { + case 0 -> 0; + default -> switch (i) { + case 100 -> 0; + default -> i; + }; + }; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedSwitchInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedSwitchInspectionTest.java new file mode 100644 index 000000000000..f16b90f8003e --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/NestedSwitchInspectionTest.java @@ -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(); + } +}