diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java index cc9a973361d0..f43aca00010e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2017 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2018 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,11 +63,11 @@ public class SwitchStatementDensityInspection extends BaseInspection { if (body == null) { return; } - final int branchCount = SwitchUtils.calculateBranchCount(statement); + final int branchCount = Math.abs(SwitchUtils.calculateBranchCount(statement)); if (branchCount == 0) { return; } - final double density = calculateDensity(body, (branchCount < 0) ? -branchCount + 1 : branchCount); + final double density = calculateDensity(body, branchCount); final int intDensity = (int)(density * 100.0); if (intDensity > m_limit) { return; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspection.java index 4cddd3d08bbb..749e15f71e20 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2017 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2018 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,11 +66,11 @@ public class SwitchStatementWithTooManyBranchesInspection extends BaseInspection @Override public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { final int branchCount = SwitchUtils.calculateBranchCount(statement); - final int branchCountIncludingDefault = (branchCount < 0) ? -branchCount + 1 : branchCount; - if (branchCountIncludingDefault <= m_limit) { + final int branchCountExcludingDefault = (branchCount < 0) ? -branchCount - 1 : branchCount; + if (branchCountExcludingDefault <= m_limit) { return; } - registerStatementError(statement, Integer.valueOf(branchCountIncludingDefault)); + registerStatementError(statement, Integer.valueOf(branchCountExcludingDefault)); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java index ae6d2f4a54d4..677ca9f93c9e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java @@ -34,9 +34,9 @@ public class SwitchUtils { private SwitchUtils() {} /** - * Calculates number of non-default cases in the specified switch statement. + * Calculates the number of branches in the specified switch statement. * When a default case is present the count will be returned as a negative number, - * e.g. if a switch statement contains 4 cases and a default case, it will return -4 + * e.g. if a switch statement contains 4 labeled cases and a default case, it will return -5 * @param statement the statement to count the cases of. * @return a negative number if a default case was encountered. */ @@ -55,7 +55,7 @@ public class SwitchUtils { branches++; } } - return defaultFound ? -branches : branches; + return defaultFound ? -branches - 1 : branches; } @Nullable diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspectionTest.java new file mode 100644 index 000000000000..129fe6efc1b5 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooManyBranchesInspectionTest.java @@ -0,0 +1,78 @@ +// 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.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class SwitchStatementWithTooManyBranchesInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doMemberTest(" public void foo(int x) {\n" + + " /*'switch' has too many branches (11)*/switch/**/ (x) {\n" + + " case 1:\n" + + " break;\n" + + " case 2:\n" + + " break;\n" + + " case 3:\n" + + " break;\n" + + " case 4:\n" + + " break;\n" + + " case 5:\n" + + " break;\n" + + " case 6:\n" + + " break;\n" + + " case 7:\n" + + " break;\n" + + " case 8:\n" + + " break;\n" + + " case 9:\n" + + " break;\n" + + " case 10:\n" + + " break;\n" + + " case 11:\n" + + " break;\n" + + " default:\n" + + " break;\n" + + " }\n" + + " }"); + } + + public void testNoWarn() { + doMemberTest(" public void foo(int x) {\n" + + " switch (x) {\n" + + " case 1:\n" + + " break;\n" + + " case 2:\n" + + " break;\n" + + " case 3:\n" + + " break;\n" + + " case 4:\n" + + " break;\n" + + " case 5:\n" + + " break;\n" + + " case 6:\n" + + " break;\n" + + " case 7:\n" + + " break;\n" + + " case 8:\n" + + " break;\n" + + " case 9:\n" + + " break;\n" + + " case 10:\n" + + " break;\n" + + " default:\n" + + " break;\n" + + " }\n" + + " }"); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new SwitchStatementWithTooManyBranchesInspection(); + } +} \ No newline at end of file