From 715f6ffa7ee65bdf3ad4d9c63c7b640384c1ea2f Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Tue, 13 Dec 2016 00:02:01 -0800 Subject: [PATCH] [infer][java] match the annotation by class name, forgetting the package, instead of checking if the end of the annotation matches Summary: Before the diff, the code was considering as Nullable any annotation ending with `...Nullable`, including `SuppressParameterNotNullable`. Closes #533 Reviewed By: jberdine Differential Revision: D4317356 fbshipit-source-id: 6091c0f --- infer/src/checkers/annotations.ml | 10 ++++------ .../java/eradicate/DefinitelyNotNullable.java | 12 ++++++++++++ .../java/eradicate/ReturnNotNullable.java | 9 +++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 infer/tests/codetoanalyze/java/eradicate/DefinitelyNotNullable.java diff --git a/infer/src/checkers/annotations.ml b/infer/src/checkers/annotations.ml index a9385c518..8a758b7ca 100644 --- a/infer/src/checkers/annotations.ml +++ b/infer/src/checkers/annotations.ml @@ -48,13 +48,11 @@ let ia_has_annotation_with ia; !found -(** Return true if [annot] ends with [ann_name] *) +(** Return true if [annot] ends with [ann_name], ignoring the package name *) let annot_ends_with annot ann_name = - let filter s = - let sl = String.length s in - let al = String.length ann_name in - sl >= al && String.sub s ~pos:(sl - al) ~len:al = ann_name in - filter annot.Annot.class_name + match String.rsplit2 annot.Annot.class_name ~on:'.' with + | None -> String.equal annot.Annot.class_name ann_name + | Some (_, annot_class_name) -> String.equal annot_class_name ann_name (** Check if there is an annotation in [ia] which ends with the given name *) let ia_ends_with ia ann_name = diff --git a/infer/tests/codetoanalyze/java/eradicate/DefinitelyNotNullable.java b/infer/tests/codetoanalyze/java/eradicate/DefinitelyNotNullable.java new file mode 100644 index 000000000..9b2e273da --- /dev/null +++ b/infer/tests/codetoanalyze/java/eradicate/DefinitelyNotNullable.java @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2013 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package codetoanalyze.java.eradicate; + +@interface DefinitelyNotNullable {} diff --git a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java index 7cab668ee..5830cb448 100644 --- a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java +++ b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java @@ -127,4 +127,13 @@ public class ReturnNotNullable { return cls.getResource(name); } + @DefinitelyNotNullable + Object definitelyDoesNotReturnNull() { + return new Object(); + } + + void callsnotnullableMethod() { + definitelyDoesNotReturnNull().toString(); + } + }