Skip to content

Commit 3a2614a

Browse files
authored
Allow instanceof to pattern matching cleanup to check inner ifs (eclipse-jdt#2108)
- fix PatternMatchingForInstanceofFixCore to continue parsing inner ifs - add new tests to CleanUpTest16 - fixes eclipse-jdt#2105
1 parent 31c8e0d commit 3a2614a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/PatternMatchingForInstanceofFixCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public boolean visit(CastExpression node) {
220220

221221
if (collector.hasResult()) {
222222
fResult.add(collector.build());
223-
return false;
223+
return true;
224224
}
225225
}
226226
return true;

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest16.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,29 @@ public int foo7(Object o) {
631631
String s = (String)o;
632632
return !(o instanceof String) ? 0 : ((String)o).length();
633633
}
634+
public int foo8(Object x, Object y, boolean b) {
635+
if (b || !(x instanceof String) || ((String)x).length() > 3) {
636+
if (y instanceof Double) {
637+
Double d = (Double)y;
638+
System.out.println(d.isNaN());
639+
}
640+
return 7;
641+
}
642+
String s = (String)x;
643+
return s.length();
644+
}
645+
public int foo9(Object x, Object y, boolean b) {
646+
if (b || !(x instanceof String) || ((String)x).length() > 3) {
647+
if (!(y instanceof Double)) {
648+
return 6;
649+
}
650+
Double d = (Double)y;
651+
System.out.println(d.isNaN());
652+
return 7;
653+
}
654+
String s = (String)x;
655+
return s.length();
656+
}
634657
}
635658
""";
636659
ICompilationUnit cu= pack.createCompilationUnit("E.java", given, false, null);
@@ -712,6 +735,25 @@ public int foo7(Object o) {
712735
}
713736
return !(o instanceof String s2) ? 0 : s2.length();
714737
}
738+
public int foo8(Object x, Object y, boolean b) {
739+
if (b || !(x instanceof String s) || s.length() > 3) {
740+
if (y instanceof Double d) {
741+
System.out.println(d.isNaN());
742+
}
743+
return 7;
744+
}
745+
return s.length();
746+
}
747+
public int foo9(Object x, Object y, boolean b) {
748+
if (b || !(x instanceof String s) || s.length() > 3) {
749+
if (!(y instanceof Double d)) {
750+
return 6;
751+
}
752+
System.out.println(d.isNaN());
753+
return 7;
754+
}
755+
return s.length();
756+
}
715757
}
716758
""";
717759

0 commit comments

Comments
 (0)