diff --git a/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml b/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml index 4dbf9c443..806dcc50b 100644 --- a/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml +++ b/infer/src/nullsafe/ThirdPartyAnnotationInfo.ml @@ -32,13 +32,20 @@ let bind_list_with_index ~init list ~f = Result.bind acc ~f:(fun acc -> f acc index elem) ) +let is_whitespace_or_comment line = + let stripped_line = String.strip line in + String.is_empty stripped_line || String.is_prefix stripped_line ~prefix:"//" + + let parse_line_and_add_to_storage signature_map ~filename ~line_index line = let open Result in - ThirdPartyMethod.parse line - >>= fun (signature, nullability) -> - Ok - ( Hashtbl.add signature_map signature {filename; line_number= line_index + 1; nullability} ; - signature_map ) + if is_whitespace_or_comment line then Ok signature_map + else + ThirdPartyMethod.parse line + >>= fun (signature, nullability) -> + Ok + ( Hashtbl.add signature_map signature {filename; line_number= line_index + 1; nullability} ; + signature_map ) let add_from_signature_file storage ~filename ~lines = diff --git a/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml b/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml index f846f142f..841488d67 100644 --- a/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml +++ b/infer/src/unit/nullsafe/ThirdPartyAnnotationInfoTests.ml @@ -91,6 +91,25 @@ let basic_find = assert_no_info storage {class_name= "a.A"; method_name= Method "foo"; param_types= ["b.B"; "c.C"]} +let disregards_whitespace_lines_and_comments = + let open ThirdPartyMethod in + "disregards_comments" + >:: fun _ -> + let lines = [" "; "a.A#foo(b.B)"; ""; "// a.A#bar(b.B)"; "// Hello world"] in + (* Load some functions from the file *) + let storage = + add_from_annot_file_and_check_success ~filename:"test.sig" + (ThirdPartyAnnotationInfo.create_storage ()) + ~lines + in + assert_has_nullability_info storage + {class_name= "a.A"; method_name= Method "foo"; param_types= ["b.B"]} + ~expected_nullability:{ret_nullability= Nonnull; param_nullability= [Nonnull]} + ~expected_file:"test.sig" ~expected_line:2 ; + (* Commented out signatures should be ignored *) + assert_no_info storage {class_name= "a.A"; method_name= Method "bar"; param_types= ["b.B"]} + + let overload_resolution = let open ThirdPartyMethod in "overload_resolution" @@ -242,6 +261,7 @@ let test_is_third_party = let test = "ThirdPartyAnnotationInfoTests" >::: [ basic_find + ; disregards_whitespace_lines_and_comments ; overload_resolution ; can_add_several_files ; should_not_forgive_unparsable_strings