[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
master
Jeremy Dubreil 7 years ago committed by Facebook Github Bot
parent b3f75da37e
commit 4283bf2602

@ -110,6 +110,9 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
when NullCheckedPname.mem callee_pname checked_pnames -> when NullCheckedPname.mem callee_pname checked_pnames ->
(* Do not report nullable when the method has already been checked for null *) (* Do not report nullable when the method has already been checked for null *)
remove_nullable_ap (ret_var, []) astate 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) | Call (Some ret_var, Direct callee_pname, _, _, loc)
when Annotations.pname_has_return_annot callee_pname when Annotations.pname_has_return_annot callee_pname
~attrs_of_pname:Specs.proc_resolve_attributes Annotations.ia_is_nullable -> ~attrs_of_pname:Specs.proc_resolve_attributes Annotations.ia_is_nullable ->

@ -8,6 +8,7 @@
*/ */
package codetoanalyze.java.checkers; package codetoanalyze.java.checkers;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class NullableViolation { 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
}
} }

Loading…
Cancel
Save