diff --git a/infer/src/backend/rearrange.ml b/infer/src/backend/rearrange.ml index 11c8fc6ce..ec6230f77 100644 --- a/infer/src/backend/rearrange.ml +++ b/infer/src/backend/rearrange.ml @@ -741,6 +741,7 @@ let add_guarded_by_constraints prop lexp pdesc = (Prop.get_exp_attributes prop guarded_by_exp) in let should_warn pdesc = Cfg.Procdesc.get_access pdesc <> Sil.Private && + not (Annotations.pdesc_has_annot pdesc Annotations.visibleForTesting) && not (Procname.java_is_access_method (Cfg.Procdesc.get_proc_name pdesc)) in match find_guarded_by_exp guarded_by_str (Prop.get_sigma prop) with | Some (Sil.Eexp (guarded_by_exp, _), typ) -> diff --git a/infer/src/checkers/annotations.ml b/infer/src/checkers/annotations.ml index 8329ba472..d325db031 100644 --- a/infer/src/checkers/annotations.ml +++ b/infer/src/checkers/annotations.ml @@ -108,6 +108,9 @@ let ma_contains ma ann_names = ) ma; !found +let pdesc_has_annot pdesc annot = + ma_contains (Cfg.Procdesc.get_attributes pdesc).ProcAttributes.method_annotation [annot] + let initializer_ = "Initializer" let inject = "Inject" let inject_view = "InjectView" diff --git a/infer/src/checkers/annotations.mli b/infer/src/checkers/annotations.mli index ab4e302d8..8fe759a30 100644 --- a/infer/src/checkers/annotations.mli +++ b/infer/src/checkers/annotations.mli @@ -103,11 +103,9 @@ val ia_is_guarded_by : Typ.item_annotation -> bool val ia_iter : (Typ.annotation -> unit) -> Typ.item_annotation -> unit -val ma_contains : Typ.method_annotation -> string list -> bool - val ma_has_annotation_with : Typ.method_annotation -> (Typ.annotation -> bool) -> bool -val ma_iter : (Typ.annotation -> unit) -> Typ.method_annotation -> unit +val pdesc_has_annot : Cfg.Procdesc.t -> string -> bool (** Mark the return of the method_annotation with the given annotation. *) val method_annotation_mark_return : diff --git a/infer/src/checkers/checkers.ml b/infer/src/checkers/checkers.ml index 83ca5c11b..f7b4f6c63 100644 --- a/infer/src/checkers/checkers.ml +++ b/infer/src/checkers/checkers.ml @@ -396,8 +396,7 @@ let callback_test_state { Callbacks.proc_name } = (** Check the uses of VisibleForTesting *) let callback_checkVisibleForTesting { Callbacks.proc_desc } = - let ma = (Specs.pdesc_resolve_attributes proc_desc).ProcAttributes.method_annotation in - if Annotations.ma_contains ma [Annotations.visibleForTesting] then + if Annotations.pdesc_has_annot proc_desc Annotations.visibleForTesting then begin let loc = Cfg.Procdesc.get_loc proc_desc in let linereader = Printer.LineReader.create () in diff --git a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java index d60ecd5c3..84c725684 100644 --- a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java +++ b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java @@ -13,6 +13,8 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import com.google.common.annotations.VisibleForTesting; + import java.io.Closeable; public class GuardedByExample { @@ -251,6 +253,16 @@ public class GuardedByExample { }; } + @VisibleForTesting + public void visibleForTestingOk1() { + f.toString(); // should push proof obl to caller + } + + @VisibleForTesting + void visibleForTestingOk2() { + f.toString(); // should push proof obl to caller + } + // TODO: report on these cases /* public void unguardedCallSiteBad1() {