From 4283bf26022111b963a68a211f9c68984a74d4eb Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Fri, 3 Nov 2017 09:28:06 -0700 Subject: [PATCH] [infer][java] re-use the Eradicate models for Preconditions.checkNotNull and the like Summary: This only works for Java at the moment but we can re-organise the code later to add the Objective C equivalent of these assertion methods. Reviewed By: mbouaziz Differential Revision: D6230588 fbshipit-source-id: 46ee98e --- infer/src/checkers/NullabilityCheck.ml | 3 +++ .../java/checkers/NullableViolation.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/infer/src/checkers/NullabilityCheck.ml b/infer/src/checkers/NullabilityCheck.ml index daa5a95af..5476b68ee 100644 --- a/infer/src/checkers/NullabilityCheck.ml +++ b/infer/src/checkers/NullabilityCheck.ml @@ -110,6 +110,9 @@ module TransferFunctions (CFG : ProcCfg.S) = struct when NullCheckedPname.mem callee_pname checked_pnames -> (* Do not report nullable when the method has already been checked for null *) remove_nullable_ap (ret_var, []) astate + | Call (_, Direct callee_pname, (HilExp.AccessPath receiver) :: _, _, _) + when Models.is_check_not_null callee_pname -> + assume_pnames_notnull receiver astate | Call (Some ret_var, Direct callee_pname, _, _, loc) when Annotations.pname_has_return_annot callee_pname ~attrs_of_pname:Specs.proc_resolve_attributes Annotations.ia_is_nullable -> diff --git a/infer/tests/codetoanalyze/java/checkers/NullableViolation.java b/infer/tests/codetoanalyze/java/checkers/NullableViolation.java index 8716fdcb9..460fd7277 100644 --- a/infer/tests/codetoanalyze/java/checkers/NullableViolation.java +++ b/infer/tests/codetoanalyze/java/checkers/NullableViolation.java @@ -8,6 +8,7 @@ */ package codetoanalyze.java.checkers; +import com.google.common.base.Preconditions; import javax.annotation.Nullable; public class NullableViolation { @@ -68,4 +69,19 @@ public class NullableViolation { } } + void usePreconditionsCheckNotNullOnVariableOkay() { + T t = returnsNullable(); + Preconditions.checkNotNull(t); + t.doSomething(); // does not report here + } + + void usePreconditionsCheckNotNullOnMethodOkay() { + Preconditions.checkNotNull(returnsNullable()).doSomething(); // does not report here + } + + void usePreconditionsCheckNotNullRepeatedCallOkay() { + Preconditions.checkNotNull(returnsNullable()); + returnsNullable().doSomething(); // does not report here + } + }