File tree 2 files changed +43
-4
lines changed
main/java/org/openrewrite/staticanalysis
test/java/org/openrewrite/staticanalysis
2 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -149,7 +149,7 @@ private static boolean breaks(Statement s) {
149
149
return !statements .isEmpty () && breaks (statements .get (statements .size () - 1 ));
150
150
} else if (s instanceof J .If ) {
151
151
J .If iff = (J .If ) s ;
152
- return iff .getElsePart () != null && breaks (iff .getThenPart ()) && breaks ( iff . getThenPart ()) ;
152
+ return iff .getElsePart () != null && breaks (iff .getThenPart ());
153
153
} else if (s instanceof J .Label ) {
154
154
return breaks (((J .Label ) s ).getStatement ());
155
155
} else if (s instanceof J .Try ) {
@@ -271,7 +271,7 @@ private static boolean returns(Statement s) {
271
271
if (value .getValue () == Boolean .TRUE ) {
272
272
Statement body = whileLoop .getBody ();
273
273
if (body instanceof J .Block ) {
274
- return hasGuaranteedReturn (((J .Block ) body ).getStatements ());
274
+ return ! hasBreak ((( J . Block ) body ). getStatements ()) && hasGuaranteedReturn (((J .Block ) body ).getStatements ());
275
275
} else {
276
276
return hasGuaranteedReturn (Collections .singletonList (whileLoop .getBody ()));
277
277
}
@@ -281,6 +281,15 @@ private static boolean returns(Statement s) {
281
281
return s instanceof J .Return ;
282
282
}
283
283
284
+ private static boolean hasBreak (List <Statement > statements ) {
285
+ for (Statement s : statements ) {
286
+ if (s instanceof J .Break ) {
287
+ return true ;
288
+ }
289
+ }
290
+ return false ;
291
+ }
292
+
284
293
@ Override
285
294
public J .Case visitCase (J .Case case_ , Set <J > ctx ) {
286
295
if (case_ == scope ) {
Original file line number Diff line number Diff line change @@ -531,7 +531,7 @@ void returnNestedInAlwaysTrueLoop() {
531
531
java (
532
532
"""
533
533
enum Enum {
534
- A, B, C, D
534
+ A, B, C, D, E, F
535
535
}
536
536
public class Test {
537
537
void foo(Enum a) {
@@ -553,14 +553,28 @@ void foo(Enum a) {
553
553
while (b) {
554
554
return;
555
555
}
556
+ case E:
557
+ for (; ; ) {
558
+ if(false) {
559
+ break;
560
+ }
561
+ return;
562
+ }
563
+ case F:
564
+ while (true) {
565
+ if(false) {
566
+ break;
567
+ }
568
+ return;
569
+ }
556
570
default:
557
571
}
558
572
}
559
573
}
560
574
""" ,
561
575
"""
562
576
enum Enum {
563
- A, B, C, D
577
+ A, B, C, D, E, F
564
578
}
565
579
public class Test {
566
580
void foo(Enum a) {
@@ -584,6 +598,22 @@ void foo(Enum a) {
584
598
return;
585
599
}
586
600
break;
601
+ case E:
602
+ for (; ; ) {
603
+ if(false) {
604
+ break;
605
+ }
606
+ return;
607
+ }
608
+ break;
609
+ case F:
610
+ while (true) {
611
+ if(false) {
612
+ break;
613
+ }
614
+ return;
615
+ }
616
+ break;
587
617
default:
588
618
}
589
619
}
You can’t perform that action at this time.
0 commit comments