diff --git a/java-checks-test-sources/default/src/main/java/checks/IdenticalCasesInSwitchCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/IdenticalCasesInSwitchCheckSample.java index ac24e37d797..adcd8f02b1e 100644 --- a/java-checks-test-sources/default/src/main/java/checks/IdenticalCasesInSwitchCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/IdenticalCasesInSwitchCheckSample.java @@ -216,4 +216,35 @@ private void f() { private void f(int i) { } + static class Parent {}; + static class A extends Parent {}; + static class B extends Parent {}; + + // Edge case involving variables introduced with `instanceof`. + // See: https://community.sonarsource.com/t/fp-java-s1871-branch-code-block-is-the-same/130645 + void variablesBoundInInstanceOf() { + Parent p1 = new A(); + Parent p2 = new B(); + if(Math.random() < 0.5) { + p1 = new B(); + p2 = new A(); + } + + // No problem when `a` refers to different variables. + Parent p = null; + if (p1 instanceof A a) { + p = a; + } + else if (p2 instanceof A a) { + p = a; + } + + // Variables have different names and identities. + if (p1 instanceof A a1) { + p = a1; + } + else if (p2 instanceof A a2) { + p = a2; + } + } } diff --git a/java-checks/src/main/java/org/sonar/java/checks/IdenticalCasesInSwitchCheck.java b/java-checks/src/main/java/org/sonar/java/checks/IdenticalCasesInSwitchCheck.java index 3bfb99d90ce..ece26539e27 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/IdenticalCasesInSwitchCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/IdenticalCasesInSwitchCheck.java @@ -131,7 +131,7 @@ private static IfElseChain collectIdenticalBranches(List allBranc for (int j = i + 1; j < allBranches.size(); j++) { StatementTree statement1 = allBranches.get(i); StatementTree statement2 = allBranches.get(j); - if (SyntacticEquivalence.areEquivalent(statement1, statement2)) { + if (SyntacticEquivalence.areEquivalentIncludingSameVariables(statement1, statement2)) { duplicates.add(statement2); ifElseChain.branches.computeIfAbsent(statement1, k -> new HashSet<>()).add(statement2); }