Skip to content

Commit bd184b7

Browse files
arodionovtimtebeek
andauthored
Fix clashing variable names generated by InstanceOfPatternMatch (#538)
* Fix clashing variable names generated by InstanceOfPatternMatch In addition to the Cursor scope, added the enclosing `J.If` scope for searching conflicting variable names. Fix #334 * Reference the right issues & rename test methods --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 6b8e3cf commit bd184b7

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java

+6
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ private String patternVariableName(J.InstanceOf instanceOf, Cursor cursor) {
320320
strategy = VariableNameStrategy.short_();
321321
}
322322
String baseName = strategy.variableName(type);
323+
if (root instanceof J.If) {
324+
J.If enclosingIf = cursor.firstEnclosing(J.If.class);
325+
String nameInIfScope = VariableNameUtils.generateVariableName(baseName, new Cursor(cursor, enclosingIf), INCREMENT_NUMBER);
326+
String nameInCursorScope = VariableNameUtils.generateVariableName(baseName, cursor, INCREMENT_NUMBER);
327+
return nameInIfScope.compareTo(nameInCursorScope) >= 0 ? nameInIfScope : nameInCursorScope;
328+
}
323329
return VariableNameUtils.generateVariableName(baseName, cursor, INCREMENT_NUMBER);
324330
}
325331

src/test/java/org/openrewrite/staticanalysis/InstanceOfPatternMatchTest.java

+90-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.openrewrite.staticanalysis;
1717

18-
import org.junit.jupiter.api.Disabled;
1918
import org.junit.jupiter.api.Nested;
2019
import org.junit.jupiter.api.Test;
2120
import org.openrewrite.Issue;
@@ -528,7 +527,6 @@ void test(Object o) {
528527
}
529528

530529
@Issue("https://github.com/openrewrite/rewrite/issues/2787")
531-
@Disabled
532530
@Test
533531
void nestedPotentiallyConflictingIfs() {
534532
rewriteRun(
@@ -549,11 +547,11 @@ void test(Object o) {
549547
"""
550548
public class A {
551549
void test(Object o) {
552-
if (o instanceof String string) {
553-
if (o instanceof String string1) {
554-
System.out.println(string1);
550+
if (o instanceof String string1) {
551+
if (o instanceof String string) {
552+
System.out.println(string);
555553
}
556-
System.out.println(string);
554+
System.out.println(string1);
557555
}
558556
}
559557
}
@@ -562,6 +560,92 @@ void test(Object o) {
562560
);
563561
}
564562

563+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/481")
564+
@Test
565+
void conflictingWithLocalVariable() {
566+
rewriteRun(
567+
//language=java
568+
java(
569+
"""
570+
public class Main {
571+
public class Bar {}
572+
public class FooBar {}
573+
public static void main(String[] args) {
574+
Object o = new Object();
575+
if (o instanceof Bar) {
576+
System.out.println(((Bar)o));
577+
Bar bar = null;
578+
if (o instanceof FooBar) {
579+
System.out.println(((FooBar)o));
580+
Bar bar1 = null;
581+
}
582+
}
583+
}
584+
}
585+
""",
586+
"""
587+
public class Main {
588+
public class Bar {}
589+
public class FooBar {}
590+
public static void main(String[] args) {
591+
Object o = new Object();
592+
if (o instanceof Bar bar2) {
593+
System.out.println(bar2);
594+
Bar bar = null;
595+
if (o instanceof FooBar fooBar) {
596+
System.out.println(fooBar);
597+
Bar bar1 = null;
598+
}
599+
}
600+
}
601+
}
602+
"""
603+
)
604+
);
605+
}
606+
607+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/334")
608+
@Test
609+
void conflictingWithOtherInstanceOf() {
610+
rewriteRun(
611+
//language=java
612+
java(
613+
"""
614+
public class Main {
615+
public class Bar {}
616+
public class FooBar {}
617+
public static void main(String[] args) {
618+
Object o = new Object();
619+
if (o instanceof Bar) {
620+
System.out.println(((Bar)o));
621+
if (o instanceof FooBar) {
622+
System.out.println(((FooBar)o));
623+
Bar bar1 = null;
624+
}
625+
}
626+
}
627+
}
628+
""",
629+
"""
630+
public class Main {
631+
public class Bar {}
632+
public class FooBar {}
633+
public static void main(String[] args) {
634+
Object o = new Object();
635+
if (o instanceof Bar bar2) {
636+
System.out.println(bar2);
637+
if (o instanceof FooBar bar) {
638+
System.out.println(bar);
639+
Bar bar1 = null;
640+
}
641+
}
642+
}
643+
}
644+
"""
645+
)
646+
);
647+
}
648+
565649
@Test
566650
void expressionWithSideEffects() {
567651
rewriteRun(

0 commit comments

Comments
 (0)