/
NullAwayNegativeCases.java
820 lines (675 loc) · 16.7 KB
/
NullAwayNegativeCases.java
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
* Copyright (c) 2017 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.nullaway.testdata;
import static com.uber.nullaway.testdata.OtherStuff.OtherEnum.TOP;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.uber.nullaway.testdata.unannotated.UnannotatedClass;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
public class NullAwayNegativeCases {
public Object doesNotReturnNull() {
return new Object();
}
public Object assignmentExpression() {
Object x = null;
return (x = new Object());
}
public void assignmentExpression2(@Nullable String[] a0) {
String[] a1;
Inner inner = new Inner();
if (((a1 = a0) != null) && (a1.length > 0)) {}
if ((null != (a1 = a0)) && (a1.length > 0)) {}
if (((inner.f1 = a0) != null) && (((String[]) inner.f1).length > 0)) {}
}
@Nullable
public Object returnsNullButAnnotatedWithNullable() {
return null;
}
public Object methodWrapper() {
return doesNotReturnNull();
}
static Object sField = new Object();
public Object returnNonNullField() {
return sField;
}
@Nullable static Object sField2 = null;
public Object checkConditionWithLocal() {
Object x = sField2;
if (x != null) {
return x;
} else {
return new Object();
}
}
public Object checkConditionOnStatic() {
if (sField2 != null) {
return sField2;
} else {
return new Object();
}
}
public Object checkConditionOnStatic2() {
if (sField2 == null) {
sField2 = new Object();
}
return sField2;
}
static class Inner2 {
@Nullable Object f3 = null;
@Nullable
public Object getF3() {
return f3;
}
}
static class Inner {
@Nullable Object f1 = null;
Object f2 = new Object();
Inner2 nested = new Inner2();
public Inner2 getNested() {
return nested;
}
public void safeWrite() {
f2 = new Object();
}
@Nullable
public Object getF1() {
return f1;
}
public void checkCondOnThis() {
if (this.f1 != null) {
this.f1.toString();
}
if (f1 != null) {
f1.toString();
}
if (this.f1 != null) {
f1.toString();
}
if (f1 != null) {
this.f1.toString();
}
if (this.f1 == null) {
} else {
this.f1.toString();
}
if (null != f1) {
this.f1.toString();
}
if (null == f1) {
} else {
f1.toString();
}
Object l = this.f1;
if (null != l) {
l.toString();
}
if (null != this.getF1()) {
this.getF1().toString();
}
}
public void checkCondOnThis2() {
if (this.f1 == null) {
return;
}
this.f1.toString();
}
public void checkCondOnThis3() {
if (this.f1 == null) {
throw new RuntimeException();
}
this.f1.toString();
}
public void checkCondOnThis4() {
if (this.f2 == null || this.f1 == null) {
return;
}
this.f1.toString();
}
public void checkCondOnThis5() {
if (this.f2 == null && this.f1 != null) {
this.f1.toString();
}
}
public void checkCondOnThis6() {
if (!(this.f1 == null)) {
this.f1.toString();
}
}
public Object lazyInit() {
if (f1 == null) {
f1 = new Object();
}
return f1;
}
public Object lazyInit2() {
if (f1 == null) {
f1 = annotatedReturn();
}
return f1;
}
public Object condExprVar() {
Object x = this.f1;
return (x == null) ? new Object() : x;
}
public Object condExpFields() {
return (this.f1 == null) ? new Object() : this.f1;
}
public Object methodInvokeCheckOnThis() {
if (getF1() == null) {
return new Object();
}
return this.getF1();
}
public Object methodInvokeCheckOnThis2() {
if (this.getF1() == null) {
return new Object();
}
return getF1();
}
public Object methodInvokeCheckOnThis3() {
if (this.getF1() == null) {
return new Object();
}
Object x = getF1();
return x;
}
public void nonNullParam(Object y) {
this.f1 = y;
this.f1.toString();
}
public void fieldNullWithCond(boolean b) {
if (f1 == null) return;
if (b) {
f1.toString();
} else {
f1.hashCode();
}
}
public void nested1() {
if (getNested().getF3() != null) {
getNested().getF3().toString();
}
if (nested.f3 != null) {
nested.f3.toString();
}
if (this.getNested().f3 != null) {
this.getNested().f3.toString();
}
}
public static void doNothing() {}
public void methodToOverride(Object o1) {}
@Nullable
public Object returnToOverride() {
return null;
}
}
static class InnerSub extends Inner {
// ok to make parameter @Nullable when @NonNull in superclass
@Override
public void methodToOverride(@Nullable Object o1) {}
// ok to return @NonNull when @Nullable in superclass
@Override
public Object returnToOverride() {
return new Object();
}
}
public Object methodInvokeCheck() {
Inner i1 = new Inner();
if (i1.getF1() == null) {
return new Object();
}
return i1.getF1();
}
public void staticInvoke() {
Inner.doNothing();
}
public Object checkNonNullInstanceField() {
Inner x = new Inner();
return x.f2;
}
public Object checkLocalStrongUpdate() {
Object x = null;
x = new Object();
Object y = x;
return y;
}
public void nonNullParam(Object x) {}
public void callNonNull() {
nonNullParam(new Object());
}
public void nonNullTwoParams(@Nullable Object x, Object y) {}
public void callNonNullTwoParams() {
nonNullTwoParams(null, new Object());
nonNullTwoParams(new Object(), new Object());
}
public static void staticMultiParam(Object x, @Nullable Object y, Object z) {}
public void callStaticMulti() {
staticMultiParam(new Object(), new Object(), new Object());
staticMultiParam(new Object(), null, new Object());
}
static void invokeOnNullWithCheck(@Nullable Object p) {
if (p != null) {
p.toString();
}
}
static void derefWithCheck(@Nullable Inner i1) {
if (i1 != null) {
Object x = i1.f1;
}
}
static void derefWithCheck2(Inner i1) {
Object x = i1.f1;
if (x != null) {
x.toString();
}
i1.f2.toString();
}
static Object multiDimArrayDeref(Object[][] multiDim) {
Object[] first = multiDim[0];
// NOTE: if we were being sound, I think this should be
// an error. Keeping this as a test just to detect
// changes in behavior
(new Inner()).f2 = multiDim[0];
Object x = first[0];
return x;
}
static boolean invokeLibMethod() {
Object x = null;
Object y = new Object();
return y.equals(x);
}
static String invokeLibMethod2() {
Object x = null;
List<Object> l = new ArrayList<>();
l.add(x);
return l.get(0).toString();
}
static void primArg(int x) {}
static void callPrimArg() {
primArg(10);
}
enum TestEnum {
VAL1,
VAL2
};
static TestEnum getOne() {
return TestEnum.VAL1;
}
static void nonNullAnnot(@Nonnull Object o) {
o.toString();
annotatedReturn().toString();
}
@Nonnull
static Object annotatedReturn() {
return new Object();
}
static void nonNullStrParam(@Nonnull String foo) {}
static void callWithLibResult() {
nonNullStrParam(String.format("%s", "hello"));
}
enum FooEnum {
FIRST,
SECOND
};
static void knownNonNull() {
nonNullAnnot(List.class);
nonNullAnnot(FooEnum.FIRST.toString());
}
static Map.Entry qualifiedClassRef() {
return new Map.Entry() {
@Override
public Object getKey() {
return new Object();
}
@Override
public Object getValue() {
return new Object();
}
@Override
public Object setValue(Object value) {
return new Object();
}
};
}
static String fromAnnotation() {
return TestAnnot.TEST_STR;
}
static Void testVoidType() {
return null;
}
static String stringConcat() {
String x = "hello ";
String y = "world";
return x + y;
}
static Object parenthesized() {
return (new Object());
}
static Object casts(boolean b) {
if (b) {
return (Object) ("hello");
} else {
return (Object) String.class;
}
}
static Boolean boxing(boolean d) {
boolean a = !d;
// nonsense
if (d) {
return a && d;
} else if (!a) {
return !d || a;
} else {
return !a;
}
}
static Number moreBoxing() {
int a = 3, b = 5;
if (b == 0) {
return b - a;
} else if (b == 1) {
return b % a;
} else if (b == 2) {
return b & a;
} else if (b == 3) {
return b | a;
} else if (b == 4) {
return b ^ a;
} else {
return 10;
}
}
static Object testStaticImport() {
if (TOP != null) {
return TOP;
}
return new Object();
}
static void outer(final Object o1) {
Runnable r =
new Runnable() {
@Override
public void run() {
o1.toString();
}
};
r.run();
}
static void iofcheck(@Nullable Object o, Inner i1) {
if (o instanceof String) {
o.hashCode();
}
if (i1.f1 instanceof String) {
i1.f1.toString();
}
if (!(i1.getF1() instanceof String)) {
return;
}
i1.getF1().toString();
}
static void checkNotNull(@Nullable Object o) {
Preconditions.checkNotNull(o);
o.toString();
}
static void checkNotNullWithMessage(@Nullable Object o) {
Preconditions.checkNotNull(o, "should not be null");
o.toString();
}
static void requireNonNull(@Nullable Object o, @Nullable Object p) {
Objects.requireNonNull(o);
o.toString();
Objects.requireNonNull(p, "should be non null");
p.toString();
}
static void isEmpty(@Nullable String s) {
if (!Strings.isNullOrEmpty(s)) {
s.hashCode();
}
}
static void checkConditionWithInstanceField() {
Inner x = new Inner();
if (x.f1 != null) {
x.f1.toString();
}
if (x.nested.getF3() != null) {
x.nested.getF3().toString();
}
}
static void tryException(@Nullable Exception exc) {
try {
if (exc != null) {
throw exc;
}
} catch (Exception e) {
e.toString();
}
}
static Object tryFinally() {
Object x = new Object();
try {
return new Object();
} finally {
if (x != null) {
return x;
}
}
}
static class ClassWithConstructor {
@Nullable Object f;
Object g;
ClassWithConstructor(@Nullable Object f, Object g) {
this.f = f;
this.g = g;
}
static ClassWithConstructor create() {
return new ClassWithConstructor(null, new Object());
}
}
static void checkAtomicRef() {
Object x = new Object();
AtomicReference<Object> ref = new AtomicReference<>(x);
if (ref.get() != null) {
ref.get().hashCode();
}
}
@Nullable private String s;
String getStr() {
return Strings.isNullOrEmpty(s) ? "hello" : s;
}
static void callArrayClone(Object[] arr) {
arr.clone();
}
@Nullable
static Boolean noUnboxingTests() {
Boolean b = null;
Boolean c = b;
Object o = c;
Boolean d = (Boolean) o;
return d;
}
static class NullableContainer {
@Nullable private Object ref;
public NullableContainer() {
ref = null;
}
@Nullable
public Object get() {
return ref;
}
public void set(Object o) {
ref = o;
}
}
@Nullable private NullableContainer nullableNullableContainer;
Object safelyGetContainerRef() {
Object saferef =
(nullableNullableContainer != null && nullableNullableContainer.get() != null)
? nullableNullableContainer.get()
: new Object();
return saferef;
}
// Ternary expressions
// From: https://github.com/google/error-prone/commit/cab5e57d5421cf70f29d231dbf704a864b4b70ae
static void g(String s, int y) {}
static void h(String s, int... y) {}
static void f(boolean b) {
int x = b ? 0 : 1;
Integer y = b ? 0 : null;
g("", b ? 1 : 0);
h("", 1, b ? 1 : 0);
h("", 1, b ? 1 : 0, 3);
int z = 0 + (b ? 0 : 1);
boolean t = Integer.valueOf(0) == (b ? 0 : null);
t = (b ? 0 : null) == Integer.valueOf(0);
}
// see https://github.com/uber/NullAway/issues/39
final class Container {
private void sender() {
int someValue = 0;
receiver(-someValue);
receiver(+someValue);
receiver(++someValue);
receiver(--someValue);
}
private void receiver(Integer i) {
/* NOP */
}
}
static class CFNullable {
@org.checkerframework.checker.nullness.qual.Nullable String cfNullableString;
CFNullable() {}
static int fizz(@org.checkerframework.checker.nullness.qual.Nullable String str) {
if (str != null) {
return str.hashCode();
}
return 10;
}
static void bizz() {
fizz(null);
}
@org.checkerframework.checker.nullness.qual.Nullable
Object retNullMaybe() {
return null;
}
}
static class CFExtends extends CFNullable {
@Override
Object retNullMaybe() {
return new Object();
}
}
static class CFNullableDecl {
@NullableDecl String cfNullableString;
CFNullableDecl() {}
static int fizz(@NullableDecl String str) {
if (str != null) {
return str.hashCode();
}
return 10;
}
static void bizz() {
fizz(null);
}
@NullableDecl
Object retNullMaybe() {
return null;
}
}
static class CFExtends2 extends CFNullableDecl {
@Override
Object retNullMaybe() {
return new Object();
}
}
static class ExcNullField {
void foo() {
UnannotatedClass e = new UnannotatedClass();
// no error since not annotated
e.maybeNull.hashCode();
UnannotatedClass f = new UnannotatedClass();
Object y = f.maybeNull;
// no error since not annotated
y.hashCode();
}
void weird() {
Class klazz = int.class;
klazz.hashCode();
}
}
static class TestAnon {
TestAnon(@Nullable Object p) {}
}
static TestAnon testAnon(@Nullable Object q) {
return new TestAnon(q) {};
}
static class TestAnon2<T> {
TestAnon2(@Nullable List<? extends T> q) {}
}
static <Q> TestAnon2<Q> testAnon2(@Nullable List<Q> q) {
return new TestAnon2<Q>(q) {};
}
static class TestAnon3 {
TestAnon3(@Nullable Integer p) {}
TestAnon3(String p) {}
}
static TestAnon3 testAnon3(@Nullable Integer q) {
return new TestAnon3(q) {};
}
// https://github.com/uber/NullAway/issues/104
static class TwoParamIterator<T, R> implements Iterator<T> {
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
return (T) new Object();
}
}
static class TwoParamCollection<T, R> implements Iterable<T> {
@Override
public TwoParamIterator<T, R> iterator() {
return new TwoParamIterator<T, R>();
}
}
static void testTwoParamIter() {
TwoParamCollection<String, String> c = new TwoParamCollection<>();
for (String s : c) {
s.hashCode();
}
}
static int testNoCrashOnShifts(int n) {
int m = n << 2;
m <<= 2;
n >>= 1;
m >>>= 4;
return (n >>> 3) + m;
}
}