java: don't report duplicate switch branches when they fall through (IDEA-223905)

GitOrigin-RevId: 9017b2cb574e41d4acb8b11f7408472823c910e3
This commit is contained in:
Bas Leijdekkers
2021-01-18 17:29:25 +00:00
committed by intellij-monorepo-bot
parent a6aea550d6
commit 2d7aef068f
3 changed files with 23 additions and 3 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2019 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.
// Copyright 2000-2021 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.intellij.codeInspection;
import com.intellij.codeInspection.util.InspectionMessage;
@@ -223,7 +223,7 @@ public class DuplicateBranchesInSwitchInspection extends LocalInspectionTool {
return previousBranch; // the code without a label is not allowed in 'switch', just ignore it
}
Branch branch = new Branch(labels, statementList, hasImplicitBreak, comments.fetchTexts());
if (previousBranch == null || !previousBranch.canFallThrough()) {
if (!branch.canFallThrough() && (previousBranch == null || !previousBranch.canFallThrough())) {
int hash = branch.hash();
List<Branch> branches = branchesByHash.get(hash);
if (branches == null) branchesByHash.put(hash, branches = new ArrayList<>());
@@ -0,0 +1,19 @@
public class DuplicateFallThrough {
public static void test(int x) {
switch (x) {
case 0:
System.out.println("a");
case 1:
System.out.println("b");
break;
case 2:
System.out.println("a");
case 3:
System.out.println("c");
break;
default:
System.out.println("d");
}
}
}
@@ -1,4 +1,4 @@
// 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.
// Copyright 2000-2021 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.intellij.java.codeInspection
import com.intellij.JavaTestUtil
@@ -25,6 +25,7 @@ class DuplicateBranchesInSwitchTest : LightJavaCodeInsightFixtureTestCase() {
fun testFallThrough() = doTest()
fun testFallThroughBefore() = doTest()
fun testAllFallThrough() = doTest()
fun testDuplicateFallThrough() = doTest()
fun testNoLastBreak() = doTest()
fun testFallThroughToBreak() = doTest()
fun testThreeDuplicates() = doTest()