diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java index d1245f15479b..48357d2b43dc 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java @@ -779,16 +779,11 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo } int hash = 0; if (depth > 0) { - int count = 0; for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof PsiWhiteSpace || child instanceof PsiComment || child instanceof PsiJavaToken) { continue; } hash = hash * 31 + hashElement(child, depth - 1); - count++; - } - if (count != 0) { - hash = hash * 31 + count; } } IElementType type = PsiUtilCore.getElementType(element); @@ -806,6 +801,9 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo return hashExpression(parenthesizedExpression.getExpression()); } short index = expression.getNode().getElementType().getIndex(); + if (expression instanceof PsiMethodReferenceExpression methodReferenceExpression) { + return hashReference(methodReferenceExpression, index) * 31 + hashElement(methodReferenceExpression.getQualifier(), 1); + } if (expression instanceof PsiReferenceExpression referenceExpression) { return hashReference(referenceExpression, index); } diff --git a/java/java-tests/testData/inspection/duplicateBranchesInSwitch/MethodReferences.java b/java/java-tests/testData/inspection/duplicateBranchesInSwitch/MethodReferences.java new file mode 100644 index 000000000000..e83eb6ef167a --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInSwitch/MethodReferences.java @@ -0,0 +1,50 @@ +package com.example; + +class Foo { + + public static void main(String[] args) { + new Foo(Mode.A).run(); + new Foo(Mode.B).run(); + } + + public interface Supplier { + T get(); + } + private final Supplier supplier; + + public Foo(Mode mode) { + supplier = switch (mode) { + case A -> A::new; + case B -> B::new; // incorrect warning happening here + }; + } + + public void run() { + supplier.get().doSomething(); + } + + public enum Mode { + A, + B + } + + private interface MyInterface { + void doSomething(); + } + + private static class A implements MyInterface { + + @Override + public void doSomething() { + System.out.println("A"); + } + } + + private static class B implements MyInterface { + + @Override + public void doSomething() { + System.out.println("B"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchSuite.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchSuite.java index f3109f351295..985e37204498 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchSuite.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInSwitchSuite.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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInspection; import org.junit.runner.RunWith; @@ -12,7 +12,8 @@ import org.junit.runners.Suite; DuplicateBranchesInEnhancedSwitchFixTest.class, DuplicateBranchesInEnhancedSwitchTest.class, DuplicateBranchesInSwitchFixTest.class, - DuplicateBranchesInSwitchTest.class + DuplicateBranchesInSwitchTest.class, + DuplicateBranchesInEnhancedSwitchFix21PreviewTest.class, }) public class DuplicateBranchesInSwitchSuite { } 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 0084aca71a87..9e1c4f896e26 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-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInspection import com.intellij.JavaTestUtil @@ -40,6 +40,7 @@ class DuplicateBranchesInSwitchTest : LightJavaCodeInsightFixtureTestCase() { fun testManySimilarBranches() = doTest() fun testParentheses() = doTest() fun testAssignment() = doTest() + fun testMethodReferences() = doTest() fun testNoExceptionWhenFirstLabelIsMissing() = doTest() fun testUnresolvedQualifier() = doTest() fun testCatchTypeReference() = doTest()