Repository navigation
Expand file tree
/
Copy pathDeadStoreCheckSample.java
More file actions
633 lines (549 loc) · 15.3 KB
/
Copy pathDeadStoreCheckSample.java
File metadata and controls
633 lines (549 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package checks;
import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.Supplier;
class DeadStoreCheckSample {
int var;
class ConstructorDeadStores {
ConstructorDeadStores(int value) {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
value = compute(); // Noncompliant {{Remove this useless assignment to local variable "value".}}
// ^^^^^^^^^^^
value = x;
var = value;
}
}
class ConstructorWithLambdaCapture {
ConstructorWithLambdaCapture(boolean condition) {
int captured = compute();
if (condition) {
System.out.println("branch");
}
Runnable runnable = () -> {
int unused = compute(); // Noncompliant {{Remove this useless assignment to local variable "unused".}}
// ^^^^^^^^^^^
System.out.println(captured);
};
runnable.run();
}
}
void singleAssignment(){
int z = 10; // Noncompliant
}
int foo(int u) {
int x = 0;// Compliant - default value
x = 3; // Noncompliant
x = 4;
int y = x + 1; // Noncompliant {{Remove this useless assignment to local variable "y".}}
// ^^^^^^^
x = 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
x = 3;
y = 2;
foo(y);
foo(x);
Object a = new Object();
System.out.println(a);
a = null; // Noncompliant
// ^^^^^^
return 0;
}
void dead_store_in_condition(BufferedReader reader) throws IOException {
String line;
if ((line = reader.readLine()) != null) { // Noncompliant {{Remove this useless assignment to local variable "line".}}
// ^^^^^^^^^^^^^^^^^^^
System.out.println("Line available");
}
}
void fields() {
this.var = 2; // Compliant
var = 3; // Compliant - do not check fields
}
void foo2() {
Object a = "";
for (int i = 0; i < 10; i++) {
a = "";
}
System.out.println(a);
}
void lambdas_using_local_vars(IntFunction lambda) {
int a = 42;
lambdas_using_local_vars(y -> a + y);
}
void dead_store_in_lambda() {
int captured = compute();
Runnable r = () -> {
int live = compute();
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
System.out.println(x + live + captured);
};
r.run();
}
IntFunction<Integer> dead_store_in_lambda_parameter() {
return x -> {
x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
return x;
};
}
IntFunction<Integer> assignment_result_used_in_block_lambda() {
return x -> {
int y = x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
return y;
};
}
IntFunction<Integer> dead_store_in_expression_lambda() {
return x -> x++; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
}
IntConsumer prefix_increment_lambda = x -> ++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
IntConsumer compound_assignment_lambda = x -> x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^
IntConsumer assignment_lambda = x -> x = 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
IntConsumer prefix_increment_block_lambda = x -> {
System.out.println(++x);
++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
};
IntConsumer compound_assignment_block_lambda = x -> {
System.out.println(x += 2);
x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^
};
IntUnaryOperator prefix_increment_result_lambda = x -> ++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
IntUnaryOperator compound_assignment_result_lambda = x -> x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
IntUnaryOperator assignment_result_lambda = x -> x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
IntUnaryOperator assignment_result_block_lambda = x -> {
return x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
};
Runnable prefix_increment_field_lambda = () -> ++var;
Runnable compound_assignment_field_lambda = () -> var += 2;
Runnable empty_lambda = () -> {};
Runnable dead_store_in_nested_lambda(boolean condition) {
return () -> {
int captured = compute();
if (condition) {
System.out.println("branch");
}
Runnable nested = () -> {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = captured;
System.out.println(x);
};
nested.run();
};
}
Supplier<Runnable> expression_lambda_returning_lambda() {
return () -> () -> {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
System.out.println(x);
};
}
Runnable classes_in_lambda_capturing_local_variables(boolean condition) {
return () -> {
int capturedByAnonymousClass = compute();
int capturedByLocalClass = compute();
class Local {
void print() {
System.out.println(capturedByLocalClass);
}
}
if (condition) {
System.out.println("branch");
}
new Runnable() {
@Override
public void run() {
System.out.println(capturedByAnonymousClass);
new Local().print();
}
}.run();
};
}
Runnable method_reference_in_lambda_using_local_variable(boolean condition) {
return () -> {
Object captured = new Object();
if (condition) {
System.out.println("branch");
}
Supplier<String> reference = captured::toString;
System.out.println(reference.get());
};
}
IntFunction<Integer> lambda_with_conditional_assignment() {
return condition -> {
int x = compute();
if (condition > 0) {
x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
}
return x;
};
}
Runnable lambda_using_local_variable_in_finally() {
return () -> {
int x = compute();
try {
x = compute();
} finally {
System.out.println(x);
}
};
}
Runnable lambda_using_local_variable_in_catch() {
return () -> {
int x = compute();
try {
x = compute();
System.out.println(x);
} catch (RuntimeException e) {
System.out.println(x);
}
};
}
Runnable lambda_with_resource_used_only_by_implicit_close() {
return () -> {
try (FileInputStream stream = new FileInputStream("...")) {
} catch (java.io.IOException e) {
throw new java.io.UncheckedIOException(e);
}
};
}
Runnable dead_store_in_lambda_inside_try_finally() {
int x = compute();
try {
return () -> {
int y = compute(); // Noncompliant {{Remove this useless assignment to local variable "y".}}
// ^^^^^^^^^^^
y = 3;
System.out.println(y);
};
} finally {
System.out.println(x);
}
}
Runnable try_finally_in_nested_lambda_should_not_exclude_enclosing_scopes() {
int unused = compute(); // Noncompliant {{Remove this useless assignment to local variable "unused".}}
// ^^^^^^^^^^^
int capturedByOuterLambda = compute();
return () -> {
int unusedInLambda = compute(); // Noncompliant {{Remove this useless assignment to local variable "unusedInLambda".}}
// ^^^^^^^^^^^
int capturedByInnerLambda = compute();
Runnable nested = () -> {
try {
System.out.println("try");
} finally {
System.out.println(capturedByOuterLambda + capturedByInnerLambda);
}
};
nested.run();
};
}
private int compute() {
return 1;
}
void ignore_try_finally() {
int a;
a = 12; //false negative excluded by try finally
try {
}finally {
a = a + 1 ;
}
}
void ignore_multiple_try_finally() {
int a;
a = 12; //false negative excluded by try finally
try {
}finally {
a = a + 1 ;
}
try {
}finally {
}
}
public boolean try_finally_return(boolean satisfied) {
try {
return satisfied = true; // compliant but by exclusion of try catch - CFG of try finally should be fixed.
} finally {
if (!satisfied) {
}
}
}
void try_finally_in_inner_class_should_not_exclude_method() {
int a;
a = 12; // Noncompliant
class inner {
void foo() {
try {
}finally {
}
}
}
}
void for_each_statement() {
int a = 0;
for (String elem: Lists.newArrayList(" ", "")) {
System.out.println(a);
a = 2;
}
System.out.println(a);
}
int read_var_in_catch() {
int a = -1;
int b = 2;
try {
a = 2;
b = raisingExceptionMethod();
System.out.println(a);
}catch (Exception e) {
System.out.println(a);
}
return b;
}
Object inner_class() {
int a = 12;
class B {
int fun() {
return a;
}
}
return new B();
}
int increment_operator() {
int i = 0;
int b = 12;
++b; // Noncompliant
// ^^^
int c = 0;
foo(++c); // Noncompliant {{Remove this useless assignment to local variable "c".}}
int j = -1;
while ((j = foo(++j)) != -1) { // Noncompliant {{Remove this useless assignment to local variable "j".}}
System.out.println("");
}
if(i != 0) {
return i++; // Noncompliant
} else {
return ++i; // Noncompliant
}
}
void parenthesis_identifier_in_assignment() {
int i = 0;
System.out.println(i);
(i) = 12; // Noncompliant
// ^^^^
}
int parenthesis_identifier_in_postfix_increment() {
int j = 0;
for (int i = 0; i < 10; ++j, ++i) ;
int b = 0;
return (b)++; // Noncompliant
// ^^^^^
}
void foo() {
int i = 0;
++i; // Noncompliant
System.out.println("");
return;
}
Object variable_initialized() {
Object foo = null; // compliant variable should be initialized
try {
foo = raisingExceptionMethod();
} catch (Exception e){
}
return foo;
}
private static int raisingExceptionMethod() throws Exception {
return 0;
}
void try_with_resource(File storageFile) throws Exception {
String path = ""; // compliant
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
br.readLine();
}
try ( FileInputStream in = new FileInputStream( storageFile );
java.nio.channels.FileLock lock = in.getChannel().lock(0, Long.MAX_VALUE, true) ) // compliant, this will be closed in the implicit finally
{
in.read();
}
}
void try_with_resource_java9() throws Exception {
final FileInputStream fis = new FileInputStream("..."); // compliant
try (fis) {
}
}
public static class MyClass {
private static class Foo {
void bar(int p){
}
}
public static void main(String... args) {
Foo x = new Foo(); // compliant
java.util.List<Integer> list = new java.util.ArrayList<>();
list.forEach(x::bar);
}
int foo() {
int i = 0;
int j = 0;
i = i + 1; // Noncompliant
j += 1; // Noncompliant
int k = 0;
k += 2;
System.out.println(k);
int n;
n = 2;
n += 12; // Noncompliant
int order = 0;
return (short) (order &= 12); // Noncompliant
}
}
private void foo3(int y) {
final int x = 1;
switch (y) {
case x:
System.out.println("1");
default:
System.out.println("2");
}
}
}
class DeadStoreCheckSampleStuff {
void foo(boolean b1, boolean b2) {
boolean x = false; // Compliant
x = b1 && b2; // Noncompliant
((x)) = b1 && b2; // Noncompliant
}
void assertStatement(boolean x) {
boolean y = !x; // compliant, y is used in assert statement.
assert y;
}
}
class DeadStoreCheckSampleNoIssueOnInitializers {
// no issue if variable initializer is 'true' or 'false'
boolean testBoolean(boolean arg0) {
boolean b1 = true; // Compliant
b1 = false; // Noncompliant
b1 = arg0;
boolean b2 = false; // Compliant
b2 = true; // Noncompliant
b2 = arg0;
boolean b3 = arg0; // Noncompliant
b3 = arg0;
return b1 && b2 && b3;
}
// no issue if initializer is 'null'
Object testNull(boolean b, Object o) {
Object o1 = null; // Compliant
o1 = new Object(); // Noncompliant
o1 = null; // Noncompliant
o1 = o;
Object o2 = o; // Noncompliant
o2 = null;
return b ? o1 : o2;
}
//no issue if initializer is the empty String
String testNull(String s) {
String s1 = ""; // Compliant
s1 = "yolo"; // Noncompliant
s1 = "hello";
String s2 = "world"; // Noncompliant
s2 = "moto";
return s1 + s2;
}
// no issue if variable initializer is '-1', '0', or '1'
int testIntLiterals() {
int a = +42; // Noncompliant
int b = (0); // Compliant
b = -1; // Noncompliant
b = 0; // Noncompliant
b = 1; // Noncompliant
int c = +1; // Compliant
int d = (-1); // Compliant
int e = -1; // Compliant
// Only int literals are excluded
long myLong = -1L; // Noncompliant
double myDouble = -1.0d; // Noncompliant
float myFloat = -1.0f; // Noncompliant
short myShort = -1; // Compliant
byte myByte = 1; //Compliant
return 0;
}
}
class DeadStoreCheckSampleB {
void foo() {
int attemptNumber = 0;
while (true) {
try {
attemptNumber++; // compliant this is handled in the catch block
throw new MyException();
} catch (MyException e) {
if (attemptNumber >= 10) {
System.exit(1);
}
}
}
}
static class MyException extends Exception { }
}
abstract class DeadStoreCheckSampleC {
public void testCodeWithForLoop2() {
RuntimeException e = null;
for (;;) {
try {
e = new RuntimeException();
break;
} finally {
doSomething();
}
}
throw e;
}
public void testCodeWithForLoop3() {
RuntimeException e = null;
for (int i = 0; i < 2; ) {
try {
e = new RuntimeException();
break;
} finally {
doSomething();
}
}
throw e;
}
public void testCodeWithWhileLoop() {
RuntimeException e = null;
while(true) {
try {
e = new RuntimeException();
break;
} finally {
doSomething();
}
}
throw e;
}
abstract void doSomething();
public class FooException extends Exception { }
}