@@ -69,31 +69,31 @@ class Test {
69
69
static void addToWhile() {
70
70
while (true) ;
71
71
}
72
-
72
+
73
73
static void addToWhileWithBody() {
74
74
while (true) return;
75
75
}
76
-
76
+
77
77
static void addToIf(int n) {
78
78
if (n == 1) return;
79
79
// foo
80
80
}
81
-
81
+
82
82
static void addToIfElse(int n) {
83
83
if (n == 1) return;
84
84
else return;
85
85
}
86
-
86
+
87
87
static void addToIfElseIfElse(int n) {
88
88
if (n == 1) return;
89
89
else if (n == 2) return;
90
90
else return;
91
91
}
92
-
92
+
93
93
static void addToDoWhile(Object obj) {
94
94
do obj.notify(); while (true);
95
95
}
96
-
96
+
97
97
static void addToIterativeFor(Object obj) {
98
98
for (int i = 0; ; ) obj.notify();
99
99
}
@@ -105,28 +105,28 @@ static void addToWhile() {
105
105
while (true) {
106
106
}
107
107
}
108
-
108
+
109
109
static void addToWhileWithBody() {
110
110
while (true) {
111
111
return;
112
112
}
113
113
}
114
-
114
+
115
115
static void addToIf(int n) {
116
116
if (n == 1) {
117
117
return;
118
118
}
119
119
// foo
120
120
}
121
-
121
+
122
122
static void addToIfElse(int n) {
123
123
if (n == 1) {
124
124
return;
125
125
} else {
126
126
return;
127
127
}
128
128
}
129
-
129
+
130
130
static void addToIfElseIfElse(int n) {
131
131
if (n == 1) {
132
132
return;
@@ -136,13 +136,13 @@ static void addToIfElseIfElse(int n) {
136
136
return;
137
137
}
138
138
}
139
-
139
+
140
140
static void addToDoWhile(Object obj) {
141
141
do {
142
142
obj.notify();
143
143
} while (true);
144
144
}
145
-
145
+
146
146
static void addToIterativeFor(Object obj) {
147
147
for (int i = 0; ; ) {
148
148
obj.notify();
@@ -165,7 +165,7 @@ class Test {
165
165
static void emptyWhile() {
166
166
while (true) ;
167
167
}
168
-
168
+
169
169
static void emptyForIterative() {
170
170
for (int i = 0; i < 10; i++) ;
171
171
}
@@ -186,26 +186,26 @@ class Test {
186
186
static void allowIf(int n) {
187
187
if (n == 1) return;
188
188
}
189
-
189
+
190
190
static void allowIfElse(int n) {
191
191
if (n == 1) return;
192
192
else return;
193
193
}
194
-
194
+
195
195
static void allowIfElseIfElse(int n) {
196
196
if (n == 1) return;
197
197
else if (n == 2) return;
198
198
else return;
199
199
}
200
-
200
+
201
201
static void allowWhileWithBody() {
202
202
while (true) return;
203
203
}
204
-
204
+
205
205
static void allowDoWhileWithBody(Object obj) {
206
206
do obj.notify(); while (true);
207
207
}
208
-
208
+
209
209
static void allowForIterativeWithBody(Object obj) {
210
210
for (int i = 0; ; ) obj.notify();
211
211
}
@@ -226,11 +226,11 @@ class Test {
226
226
static void doNotAllowWhileWithEmptyBody() {
227
227
while (true) ;
228
228
}
229
-
229
+
230
230
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
231
231
do ; while (true);
232
232
}
233
-
233
+
234
234
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
235
235
for (int i = 0; ; ) ;
236
236
}
@@ -242,12 +242,12 @@ static void doNotAllowWhileWithEmptyBody() {
242
242
while (true) {
243
243
}
244
244
}
245
-
245
+
246
246
static void doNotAllowDoWhileWithEmptyBody(Object obj) {
247
247
do {
248
248
} while (true);
249
249
}
250
-
250
+
251
251
static void doNotAllowForIterativeWithEmptyBody(Object obj) {
252
252
for (int i = 0; ; ) {
253
253
}
@@ -311,6 +311,15 @@ static void method() {
311
311
if (true) return; // comment 2
312
312
return;
313
313
}
314
+ static void commentWhile(Object obj) {
315
+ while (true); // while comment
316
+ while (true) return; // while comment with body
317
+ do obj.notify(); while (true); // do while comment
318
+ }
319
+ static void commentIterative(Object obj) {
320
+ for (int i = 0; ; ); // iterative comment
321
+ for (int i = 0; ; ) obj.notify(); // iterative with body comment
322
+ }
314
323
}
315
324
""" ,
316
325
"""
@@ -324,6 +333,177 @@ static void method() {
324
333
}
325
334
return;
326
335
}
336
+ static void commentWhile(Object obj) {
337
+ while (true) { // while comment
338
+ }
339
+ while (true) {
340
+ return; // while comment with body
341
+ }
342
+ do {
343
+ obj.notify(); // do while comment
344
+ } while (true);
345
+ }
346
+ static void commentIterative(Object obj) {
347
+ for (int i = 0; ; ) { // iterative comment
348
+ }
349
+ for (int i = 0; ; ) {
350
+ obj.notify(); // iterative with body comment
351
+ }
352
+ }
353
+ }
354
+ """
355
+ )
356
+ );
357
+ }
358
+
359
+ @ Issue ("https://github.com/openrewrite/rewrite-static-analysis/issues/315" )
360
+ @ Test
361
+ void commentsBeforeElseBlockWithNoBraces () {
362
+ rewriteRun (
363
+ //language=java
364
+ java (
365
+ """
366
+ class Test {
367
+ static void method() {
368
+ if (true) {
369
+ return;
370
+ }
371
+ /*
372
+ * comment should be in else block
373
+ */
374
+ else return;
375
+ if(true) {
376
+ return;
377
+ } // comment on if
378
+ else return;
379
+ }
380
+ }
381
+ """ ,
382
+ """
383
+ class Test {
384
+ static void method() {
385
+ if (true) {
386
+ return;
387
+ } else {
388
+ /*
389
+ * comment should be in else block
390
+ */
391
+ return;
392
+ }
393
+ if(true) {
394
+ return;
395
+ } // comment on if
396
+ else {
397
+ return;
398
+ }
399
+ }
400
+ }
401
+ """
402
+ )
403
+ );
404
+ }
405
+
406
+ @ Issue ("https://github.com/openrewrite/rewrite-static-analysis/issues/315" )
407
+ @ Test
408
+ void commentsBeforeElseBlockWithBraces () {
409
+ rewriteRun (
410
+ //language=java
411
+ java (
412
+ """
413
+ class Test {
414
+ static void method() {
415
+ if (true)
416
+ return;
417
+ // if comment
418
+ else{
419
+ return;
420
+ }
421
+ }
422
+ }
423
+ """ ,
424
+ """
425
+ class Test {
426
+ static void method() {
427
+ if (true) {
428
+ return;
429
+ // if comment
430
+ } else {
431
+ return;
432
+ }
433
+ }
434
+ }
435
+ """
436
+ )
437
+ );
438
+ }
439
+
440
+ @ Issue ("https://github.com/openrewrite/rewrite-static-analysis/issues/315" )
441
+ @ Test
442
+ void trailingCommentsElseBlock () {
443
+ rewriteRun (
444
+ //language=java
445
+ java (
446
+ """
447
+ class Test {
448
+ static void method() {
449
+ if (true) return; // if comment
450
+ else return; // else comment
451
+ if (true) return; // if comment 2
452
+ else if (true) return; // else if comment 2
453
+ else if (true) return; // second else if comment 2
454
+ else return; // else comment 2
455
+ }
456
+ static void methodTwo(){
457
+ if (true) return; // if comment
458
+ else return; // else comment
459
+ return; // return comment
460
+ }
461
+ static void methodThreeNested(){
462
+ if (true){
463
+ if (true) return; // nested if comment
464
+ else return; // nested else comment
465
+ }
466
+ else return; // else comment
467
+ }
468
+ }
469
+ """ ,
470
+ """
471
+ class Test {
472
+ static void method() {
473
+ if (true) {
474
+ return; // if comment
475
+ } else {
476
+ return; // else comment
477
+ }
478
+ if (true) {
479
+ return; // if comment 2
480
+ } else if (true) {
481
+ return; // else if comment 2
482
+ } else if (true) {
483
+ return; // second else if comment 2
484
+ } else {
485
+ return; // else comment 2
486
+ }
487
+ }
488
+ static void methodTwo(){
489
+ if (true) {
490
+ return; // if comment
491
+ } else {
492
+ return; // else comment
493
+ }
494
+ return; // return comment
495
+ }
496
+ static void methodThreeNested(){
497
+ if (true) {
498
+ if (true) {
499
+ return; // nested if comment
500
+ } else {
501
+ return; // nested else comment
502
+ }
503
+ } else {
504
+ return; // else comment
505
+ }
506
+ }
327
507
}
328
508
"""
329
509
)
0 commit comments