[nullsafe] Make intention of NullFieldAccess test explicit and add positive cases

Summary:
Get rid of helper class `C`, normal Object serves the same goal well
Don't return values from a function, focus only on nullable
dereferences.

Reviewed By: jberdine

Differential Revision: D17314569

fbshipit-source-id: d70e66b5f
master
Mitya Lyubarskiy 5 years ago committed by Facebook Github Bot
parent 3dc0c5938f
commit 224d44d79a

@ -10,49 +10,58 @@ package codetoanalyze.java.nullsafe_default;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class NullFieldAccess { public class NullFieldAccess {
class C {
int n;
}
interface I { interface I {
@Nullable C c = null; @Nullable Object nullable = new Object();
Object notNull = new Object();
} }
@Nullable C x; @Nullable Object nullable;
C y; Object notNull;
static final @Nullable C z = null;
static final @Nullable Object nullableStatic = new Object();
static final Object notNullStatic = new Object();
@Nullable Object[] nullableArray;
Object[] notNullArray;
NullFieldAccess() { NullFieldAccess() {
y = new C(); nullable = new Object();
notNull = new Object();
nullableArray = new Object[1];
notNullArray = new Object[1];
} }
int useX() { void testNonStaticFields() {
C c = x; Object bad = nullable;
return c.n; bad.toString(); // BAD: `bad` can be null
}
int useY() { Object good = notNull;
C c = y; good.toString(); // OK: `good` is not null
return c.n;
} }
int useZ() { void testStatic() {
C c = z; Object bad = nullableStatic;
return c.n; bad.toString(); // BAD: `bad` can be null
}
int useInterface(I i) { Object good = notNullStatic;
C c = i.c; good.toString(); // OK: `good` is not null
return c.n;
} }
@Nullable Object[] objects; void testInterface() {
Object bad = I.nullable;
bad.toString(); // BAD: `bad` can be null
int arrayLength() { Object good = I.notNull;
return objects.length; good.toString(); // OK: `good` is not null
} }
Object arrayAccess() { void testArray() {
return objects[0]; int i1 = nullableArray.length; // BAD: array can be null
Object o1 = nullableArray[0]; // BAD: array can be null
int i2 = notNullArray.length; // OK: arrays is not null
Object o2 = notNullArray[0]; // OK: array is not null
} }
} }

@ -102,11 +102,11 @@ codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.n
codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.param_AccessWithoutNullCheckIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$C):void, 1, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 65)] codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.param_AccessWithoutNullCheckIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$C):void, 1, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 65)]
codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.veryDeep_AccessWithoutNullCheckIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$CCC):void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `ccc.cc.c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 100)] codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.veryDeep_AccessWithoutNullCheckIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$CCC):void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `ccc.cc.c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 100)]
codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.veryDeep_IncompleteAccessViaOrEarlyReturnIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$CCC):void, 3, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `ccc.cc.c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 114)] codetoanalyze/java/nullsafe-default/NestedFieldAccess.java, codetoanalyze.java.nullsafe_default.NestedFieldAccess$TestNullableChains.veryDeep_IncompleteAccessViaOrEarlyReturnIsBad(codetoanalyze.java.nullsafe_default.NestedFieldAccess$CCC):void, 3, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `ccc.cc.c.s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NestedFieldAccess$C.s at line 114)]
codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.arrayAccess():java.lang.Object, 1, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `NullFieldAccess.objects` is nullable and is not locally checked for null when accessing element at index `0`. (Origin: field NullFieldAccess.objects at line 56)] codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.testArray():void, 1, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `NullFieldAccess.nullableArray` is nullable and is not locally checked for null when accessing field `length`. (Origin: field NullFieldAccess.nullableArray at line 60)]
codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.arrayLength():int, 1, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `NullFieldAccess.objects` is nullable and is not locally checked for null when accessing field `length`. (Origin: field NullFieldAccess.objects at line 52)] codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.testArray():void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `NullFieldAccess.nullableArray` is nullable and is not locally checked for null when accessing element at index `0`. (Origin: field NullFieldAccess.nullableArray at line 60)]
codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.useInterface(codetoanalyze.java.nullsafe_default.NullFieldAccess$I):int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `c` is nullable and is not locally checked for null when accessing field `NullFieldAccess$C.n`. (Origin: field NullFieldAccess$I.c at line 45)] codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.testInterface():void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `bad` in the call to `toString()` is nullable and is not locally checked for null. (Origin: field NullFieldAccess$I.nullable at line 52)]
codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.useX():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `c` is nullable and is not locally checked for null when accessing field `NullFieldAccess$C.n`. (Origin: field NullFieldAccess.x at line 30)] codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.testNonStaticFields():void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `bad` in the call to `toString()` is nullable and is not locally checked for null. (Origin: field NullFieldAccess.nullable at line 36)]
codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.useZ():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [Object `c` is nullable and is not locally checked for null when accessing field `NullFieldAccess$C.n`. (Origin: field NullFieldAccess.z at line 40)] codetoanalyze/java/nullsafe-default/NullFieldAccess.java, codetoanalyze.java.nullsafe_default.NullFieldAccess.testStatic():void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `bad` in the call to `toString()` is nullable and is not locally checked for null. (Origin: field NullFieldAccess.nullableStatic at line 44)]
codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall$Inner.outerField():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.fld at line 69)] codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall$Inner.outerField():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.fld at line 69)]
codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall$Inner.outerPrivateField():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.pfld at line 80)] codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall$Inner.outerPrivateField():int, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.pfld at line 80)]
codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall.FP_propagatesNonNullAfterComparisonFieldOkay(java.lang.Object):void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `NullMethodCall.nullableField` in the call to `toString()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.nullableField at line 275)] codetoanalyze/java/nullsafe-default/NullMethodCall.java, codetoanalyze.java.nullsafe_default.NullMethodCall.FP_propagatesNonNullAfterComparisonFieldOkay(java.lang.Object):void, 2, ERADICATE_NULLABLE_DEREFERENCE, no_bucket, WARNING, [The value of `NullMethodCall.nullableField` in the call to `toString()` is nullable and is not locally checked for null. (Origin: field NullMethodCall.nullableField at line 275)]

Loading…
Cancel
Save