[infer][java] Infer should not suggest @Nullable on captured parameters

Summary:
This just makes the warnings silent for now. We may improve the analysis to check if the null check on the captured fields are consistent with the annotation on the corresponding parameters.

Eradicate also has the same issue. I added a test to outline this. The biabduction analysis will also probably fail on the same of annotation lookup. We may want implement the proper fix at the level of `Annotation.field_has_annot`.

Reviewed By: sblackshear

Differential Revision: D5419243

fbshipit-source-id: 6460de8
master
Jeremy Dubreil 7 years ago committed by Facebook Github Bot
parent 297254d15e
commit bafd1cf412

@ -1092,14 +1092,6 @@ module Fieldname = struct
module Set = Caml.Set.Make (T)
module Map = Caml.Map.Make (T)
module Clang = struct
let from_class_name class_name field_name = Clang {class_name; field_name}
end
module Java = struct
let from_string n = Java n
end
(** Convert a fieldname to a string. *)
let to_string = function
| Hidden
@ -1181,6 +1173,21 @@ module Fieldname = struct
(** hidded fieldname constant *)
let is_hidden fn = equal fn hidden
module Clang = struct
let from_class_name class_name field_name = Clang {class_name; field_name}
end
module Java = struct
let from_string n = Java n
let is_captured_parameter field_name =
match field_name with
| Java _
-> String.is_prefix ~prefix:"val$" (to_flat_string field_name)
| Hidden | Clang _
-> false
end
end
module Struct = struct

@ -509,6 +509,9 @@ module Fieldname : sig
module Java : sig
val from_string : string -> t
(** Create a java field name from string *)
val is_captured_parameter : t -> bool
(** Check if field is a captured parameter *)
end
val to_string : t -> string

@ -148,6 +148,9 @@ let checker {Callbacks.summary; proc_desc; tenv} =
let report_access_path ap udchain =
let issue_kind = Localise.to_issue_id Localise.field_should_be_nullable in
match AccessPath.Raw.get_field_and_annotation ap proc_data.tenv with
| Some (field_name, _) when Typ.Fieldname.Java.is_captured_parameter field_name
-> (* Skip reporting when field comes from generated code *)
()
| Some (field_name, _)
-> (
let message =

@ -105,4 +105,25 @@ public class NullableSuggest {
// Pretend that we did something here...
}
}
void methodWithCapturedNullableParameterOk(@Nullable Object parameter) {
Object object = new Object() {
void foo() {
if (parameter != null) {
parameter.toString();
}
}
};
}
void methodWithCapturednonNullableParameterBad_FN(Object parameter) {
Object object = new Object() {
void foo() {
if (parameter != null) {
parameter.toString();
}
}
};
}
}

@ -350,4 +350,12 @@ class NestedFieldAccess {
}
}
void methodWithNullableCapturedParameterBad_FN(@Nullable Object parameter) {
Object object = new Object() {
void foo() {
parameter.toString();
}
};
}
}

Loading…
Cancel
Save