mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: count switch cases correctly
(to match "'switch' statement with too few branches" changes
This commit is contained in:
+3
-3
@@ -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;
|
||||
|
||||
+4
-4
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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
|
||||
|
||||
+78
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user