Summary: Follow ups will include error messaging that makes the choice clear Reviewed By: artempyanykh Differential Revision: D18347664 fbshipit-source-id: b6f005726master
parent
3d2df4cc3c
commit
027ff479d1
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package codetoanalyze.java.nullsafe_default;
|
||||||
|
|
||||||
|
import com.facebook.infer.annotation.NullsafeStrict;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import some.test.pckg.ThirdPartyTestClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In this test, we test how Strict mode works for calls of 3rd party libraries, and how detection
|
||||||
|
* differs based on if the function is whitelisted or not in 3rd party signatures repository.
|
||||||
|
*/
|
||||||
|
@NullsafeStrict
|
||||||
|
public class StrictModeForThirdParty {
|
||||||
|
|
||||||
|
ThirdPartyTestClass obj;
|
||||||
|
|
||||||
|
StrictModeForThirdParty() {
|
||||||
|
obj = new ThirdPartyTestClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String getNullable() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNonnull() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return values.
|
||||||
|
// In strict mode, return values should be pessimistically treated as nullable
|
||||||
|
// if the function is unspecified, and treated according to their return annotation if
|
||||||
|
// the function is whitelisted in the 3rd party repo.
|
||||||
|
|
||||||
|
public void dereferenceUnspecifiedIsBAD() {
|
||||||
|
obj.returnUnspecified().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dereferenceSpecifiedAsNullableIsBAD() {
|
||||||
|
obj.returnSpecifiedAsNullable().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dereferenceSpecifiedAsNonnullIsOK() {
|
||||||
|
obj.returnSpecifiedAsNonnull().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Params.
|
||||||
|
// In strict mode, params should be pessimistically treated as non-nullable if the function is
|
||||||
|
// unspecified,
|
||||||
|
// and treated based on their annotation if the function is whitelisted in the 3rd party repo.
|
||||||
|
|
||||||
|
public void passingNullableParamToUnspecifiedIsBAD() {
|
||||||
|
obj.paramUnspecified(getNullable());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void passingNonnullParamToUnspecifiedIsOK() {
|
||||||
|
obj.paramUnspecified(getNonnull());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void passingNullableToParamSpecifiedAsNonnullIsBAD() {
|
||||||
|
obj.secondParamSpecifiedAsNonnull(getNonnull(), getNullable());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void passingNullableToParamSpecifiedAsNullableIsOK() {
|
||||||
|
// first param is explicitly whitelisted as specified as nullable, so everything is OK
|
||||||
|
obj.secondParamSpecifiedAsNonnull(getNullable(), getNonnull());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void passingNonnullToParamIsOK() {
|
||||||
|
// Independently of param signature, it is safe to pass non-nullables
|
||||||
|
obj.secondParamSpecifiedAsNonnull(getNonnull(), getNonnull());
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +0,0 @@
|
|||||||
some.test.pckg.SomeClass#getNullable() @Nullable
|
|
||||||
some.test.pckg.SomeClass#getNonnull()
|
|
@ -0,0 +1,3 @@
|
|||||||
|
some.test.pckg.ThirdPartyTestClass#returnSpecifiedAsNonnull()
|
||||||
|
some.test.pckg.ThirdPartyTestClass#returnSpecifiedAsNullable() @Nullable
|
||||||
|
some.test.pckg.ThirdPartyTestClass#secondParamSpecifiedAsNonnull(@Nullable java.lang.String, java.lang.String)
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
package some.test.pckg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A test third party class. We specify its annotations outside of this class, in a third-party
|
||||||
|
* repository.
|
||||||
|
*/
|
||||||
|
public class ThirdPartyTestClass {
|
||||||
|
|
||||||
|
// Return values.
|
||||||
|
|
||||||
|
// No information in 3rd party repo
|
||||||
|
public String returnUnspecified() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3rd party repo whitelists this function as returning non-nullable
|
||||||
|
public String returnSpecifiedAsNonnull() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3rd party repo whitelists this function as returning nullable
|
||||||
|
public String returnSpecifiedAsNullable() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Params.
|
||||||
|
|
||||||
|
// No information about this function in 3rd party repo
|
||||||
|
public void paramUnspecified(String param) {}
|
||||||
|
|
||||||
|
public void secondParamSpecifiedAsNonnull(
|
||||||
|
String specifiedAsNullable, String specifiedAsNonnull) {}
|
||||||
|
}
|
Loading…
Reference in new issue