From 2d7aef068f904bf06dbadf4044dd5efdc4ae4937 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 18 Jan 2021 11:08:04 +0100 Subject: [PATCH] java: don't report duplicate switch branches when they fall through (IDEA-223905) GitOrigin-RevId: 9017b2cb574e41d4acb8b11f7408472823c910e3 --- .../DuplicateBranchesInSwitchInspection.java | 4 ++-- .../DuplicateFallThrough.java | 19 +++++++++++++++++++ .../DuplicateBranchesInSwitchTest.kt | 3 ++- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/inspection/duplicateBranchesInSwitch/DuplicateFallThrough.java diff --git a/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java b/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java index a143bd89c64f..6988ec610c44 100644 --- a/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java @@ -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 branches = branchesByHash.get(hash); if (branches == null) branchesByHash.put(hash, branches = new ArrayList<>()); diff --git a/java/java-tests/testData/inspection/duplicateBranchesInSwitch/DuplicateFallThrough.java b/java/java-tests/testData/inspection/duplicateBranchesInSwitch/DuplicateFallThrough.java new file mode 100644 index 000000000000..5e632d997826 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInSwitch/DuplicateFallThrough.java @@ -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"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchTest.kt index 4dd14188609e..ac90db79a86b 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchTest.kt @@ -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()