From e801617488a7591428cc26cc8da4e7dfaa1031f7 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Tue, 20 Mar 2018 14:05:59 -0700 Subject: [PATCH] [infer][java] Eradicate should not report a Return Not Nullable when a method returns the integer 0 Summary: This was causing false positives when returning the constant integer 0. Reviewed By: sblackshear Differential Revision: D7330143 fbshipit-source-id: 45d19dd --- infer/src/eradicate/typeCheck.ml | 12 +++++------- .../java/eradicate/ReturnNotNullable.java | 8 ++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/infer/src/eradicate/typeCheck.ml b/infer/src/eradicate/typeCheck.ml index ab77d6c68..567413adc 100644 --- a/infer/src/eradicate/typeCheck.ml +++ b/infer/src/eradicate/typeCheck.ml @@ -147,6 +147,11 @@ type checks = {eradicate: bool; check_extension: bool; check_ret_type: check_ret let rec typecheck_expr find_canonical_duplicate visited checks tenv node instr_ref (curr_pdesc: Procdesc.t) typestate e tr_default loc : TypeState.range = match e with + | _ when Exp.is_null_literal e -> + let typ, ta, locs = tr_default in + if PatternMatch.type_is_class typ then + (typ, TypeAnnotation.const AnnotatedSignature.Nullable true (TypeOrigin.Const loc), locs) + else (typ, TypeAnnotation.with_origin ta (TypeOrigin.Const loc), locs) | Exp.Lvar pvar -> ( match TypeState.lookup_pvar pvar typestate with | Some tr -> @@ -159,13 +164,6 @@ let rec typecheck_expr find_canonical_duplicate visited checks tenv node instr_r TypeState.range_add_locs tr [loc] | None -> tr_default ) - | Exp.Const Const.Cint i when IntLit.iszero i -> - let typ, _, locs = tr_default in - if PatternMatch.type_is_class typ then - (typ, TypeAnnotation.const AnnotatedSignature.Nullable true (TypeOrigin.Const loc), locs) - else - let t, ta, ll = tr_default in - (t, TypeAnnotation.with_origin ta (TypeOrigin.Const loc), ll) | Exp.Exn e1 -> typecheck_expr find_canonical_duplicate visited checks tenv node instr_ref curr_pdesc typestate e1 tr_default loc diff --git a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java index 70f0f5791..ae1428a4d 100644 --- a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java +++ b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java @@ -155,4 +155,12 @@ public class ReturnNotNullable { Object $generatedReturnsNullOk() { return null; } + + int field; + + int returnsZero() { + field = 0; + return field; + } + }