From 20c8c7680a72b6267a618d9ab1e6d52567b4405a Mon Sep 17 00:00:00 2001 From: Tomasz Tylenda Date: Fri, 22 Nov 2024 12:00:05 +0100 Subject: [PATCH] S1871 Consider variable identity when testing branch equivalence. --- .../main/java/checks/IdenticalCasesInSwitchCheckSample.java | 4 +--- .../org/sonar/java/checks/IdenticalCasesInSwitchCheck.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) 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 9314a4d6573..a890c6a0b5d 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 @@ -232,11 +232,9 @@ void variablesBoundInInstanceOf() { Parent p = null; if (p1 instanceof A a) { -// ^[el=+3;ec=5]> 1 {{Original}} p = a; } - else if (p2 instanceof A a) { // Noncompliant {{This branch's code block is the same as the block for the branch on line 234.}} -// ^[el=+3;ec=5] + else if (p2 instanceof A a) { p = a; } 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 391c261981e..229d7747822 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 @@ -134,7 +134,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); }