From d08cabe7edb322d7fa2837473eab5727273ddbd0 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Wed, 6 Feb 2019 07:52:06 -0800 Subject: [PATCH] [eradicate] match the different re-definitions of Preconditions.checkState Reviewed By: mbouaziz Differential Revision: D13966540 fbshipit-source-id: 3cdbb3fbc --- infer/src/nullsafe/models.ml | 8 ++++++-- .../codetoanalyze/java/eradicate/MyPreconditions.java | 4 ++++ .../codetoanalyze/java/eradicate/NullMethodCall.java | 10 ++++++++++ infer/tests/codetoanalyze/java/eradicate/issues.exp | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/infer/src/nullsafe/models.ml b/infer/src/nullsafe/models.ml index 740030b61..787ee6266 100644 --- a/infer/src/nullsafe/models.ml +++ b/infer/src/nullsafe/models.ml @@ -68,10 +68,14 @@ let get_check_not_null_parameter proc_name = (** Check if the procedure is one of the known Preconditions.checkState. *) -let is_check_state proc_name = table_has_procedure check_state_table proc_name +let is_check_state proc_name = + table_has_procedure check_state_table proc_name || match_method_name proc_name "checkState" + (** Check if the procedure is one of the known Preconditions.checkArgument. *) -let is_check_argument proc_name = table_has_procedure check_argument_table proc_name +let is_check_argument proc_name = + table_has_procedure check_argument_table proc_name || match_method_name proc_name "checkArgument" + (** Check if the procedure does not return. *) let is_noreturn proc_name = table_has_procedure noreturn_table proc_name diff --git a/infer/tests/codetoanalyze/java/eradicate/MyPreconditions.java b/infer/tests/codetoanalyze/java/eradicate/MyPreconditions.java index ca6d510f4..02e174122 100644 --- a/infer/tests/codetoanalyze/java/eradicate/MyPreconditions.java +++ b/infer/tests/codetoanalyze/java/eradicate/MyPreconditions.java @@ -11,4 +11,8 @@ import javax.annotation.Nullable; public class MyPreconditions { public static native T checkNotNull(@Nullable T t); + + public static native void checkState(boolean expression); + + public static native void checkArgument(boolean expression); } diff --git a/infer/tests/codetoanalyze/java/eradicate/NullMethodCall.java b/infer/tests/codetoanalyze/java/eradicate/NullMethodCall.java index 79770ff08..624bd9bbb 100644 --- a/infer/tests/codetoanalyze/java/eradicate/NullMethodCall.java +++ b/infer/tests/codetoanalyze/java/eradicate/NullMethodCall.java @@ -287,6 +287,16 @@ public class NullMethodCall { return nullableField.toString(); } + String customPreconditionsCheckStateOkay() { + MyPreconditions.checkState(nullableField != null); + return nullableField.toString(); + } + + String customPreconditionsCheckArgumentOkay(@Nullable Object arg) { + MyPreconditions.checkState(arg != null); + return arg.toString(); + } + void nullMethodCallWithAlarmManager(AlarmManager manager, @Nullable PendingIntent intent) { manager.cancel(intent); } diff --git a/infer/tests/codetoanalyze/java/eradicate/issues.exp b/infer/tests/codetoanalyze/java/eradicate/issues.exp index 957fbfc80..4c7ac5093 100644 --- a/infer/tests/codetoanalyze/java/eradicate/issues.exp +++ b/infer/tests/codetoanalyze/java/eradicate/issues.exp @@ -51,7 +51,7 @@ codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.N codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.FP_propagatesNonNullAfterComparisonFieldOkay(java.lang.Object):void, 2, ERADICATE_NULL_METHOD_CALL, no_bucket, WARNING, [origin,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 273)] codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.FP_propagatesNonNullAfterComparisonParameterOkay(java.lang.Object,java.lang.Object):void, 3, ERADICATE_NULL_METHOD_CALL, no_bucket, WARNING, [The value of `nullableParameter` in the call to `toString()` is nullable and is not locally checked for null. (Origin: method parameter nullableParameter)] codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.callOnNull():void, 2, ERADICATE_NULL_METHOD_CALL, no_bucket, WARNING, [origin,The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: null constant at line 23)] -codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.callingSeverSideNullableGetter(codetoanalyze.java.eradicate.ServerSideDeserializer):java.lang.String, 1, ERADICATE_NULL_METHOD_CALL, no_bucket, ERROR, [origin,The value of `deserializer.nullableGetter()` in the call to `toString()` is nullable and is not locally checked for null. (Origin: call to nullableGetter() at line 295)] +codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.callingSeverSideNullableGetter(codetoanalyze.java.eradicate.ServerSideDeserializer):java.lang.String, 1, ERADICATE_NULL_METHOD_CALL, no_bucket, ERROR, [origin,The value of `deserializer.nullableGetter()` in the call to `toString()` is nullable and is not locally checked for null. (Origin: call to nullableGetter() at line 305)] codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.nullMethodCallWithAlarmManager(android.app.AlarmManager,android.app.PendingIntent):void, 1, ERADICATE_PARAMETER_NOT_NULLABLE, no_bucket, WARNING, [`AlarmManager.cancel(...)` needs a non-null value in parameter 1 but argument `intent` can be null. (Origin: method parameter intent)] codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.testExceptionPerInstruction(int):void, 6, ERADICATE_NULL_METHOD_CALL, no_bucket, WARNING, [origin,The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: null constant at line 179)] codetoanalyze/java/eradicate/NullMethodCall.java, codetoanalyze.java.eradicate.NullMethodCall.testFieldAssignmentIfThenElse(java.lang.String):void, 2, ERADICATE_NULL_METHOD_CALL, no_bucket, WARNING, [origin,The value of `s` in the call to `length()` is nullable and is not locally checked for null. (Origin: null constant at line 170)]