Java: fix "Duplicate branches in 'switch'" inspection false positive on method reference (IDEA-355483)

GitOrigin-RevId: 0d5e06632a349494f05b79dab40d37988fdd7412
This commit is contained in:
Bas Leijdekkers
2024-10-03 18:24:19 +00:00
committed by intellij-monorepo-bot
parent 772de1b8c2
commit 2f23d849ea
4 changed files with 58 additions and 8 deletions
@@ -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);
}
@@ -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> {
T get();
}
private final Supplier<MyInterface> 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");
}
}
}
@@ -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 {
}
@@ -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()