Skip to content

Commit

Permalink
S1871 Add reported FP to test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-tylenda-sonarsource committed Nov 25, 2024
1 parent 8994f83 commit 8322ed0
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,36 @@ private void f() {
private void f(int i) {
}

static class Parent {};
static class A extends Parent {};
static class B extends Parent {};

// Edge case FP.
// 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();
}

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]
p = a;
}

// Renaming variables fixes the issue.
if (p1 instanceof A a1) {
p = a1;
}
else if (p2 instanceof A a2) {
p = a2;
}
}
}

0 comments on commit 8322ed0

Please sign in to comment.