diff --git a/infer/src/backend/rearrange.ml b/infer/src/backend/rearrange.ml index 9c8a33a82..ddcda577a 100644 --- a/infer/src/backend/rearrange.ml +++ b/infer/src/backend/rearrange.ml @@ -713,6 +713,9 @@ let add_guarded_by_constraints prop lexp pdesc = | Sil.Alocked -> true | _ -> false) (Prop.get_exp_attributes prop guarded_by_exp) in + let should_warn pdesc = + Cfg.Procdesc.get_access pdesc <> Sil.Private && + 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) -> if is_read_write_lock typ @@ -726,7 +729,7 @@ let add_guarded_by_constraints prop lexp pdesc = prop else (* we don't know if we have the lock or not. *) - if Cfg.Procdesc.get_access pdesc <> Sil.Private + if should_warn pdesc then begin (* non-private method; can't ensure that the lock is held. warn. *) @@ -739,7 +742,7 @@ let add_guarded_by_constraints prop lexp pdesc = Prop.conjoin_neq ~footprint:true guarded_by_exp locked_attr prop | _ -> if not (proc_has_matching_annot pdesc guarded_by_str - || is_synchronized_on_class guarded_by_str) + || is_synchronized_on_class guarded_by_str) && should_warn pdesc then (* can't find the object the annotation refers to, and procedure is not annotated. warn *) warn accessed_fld guarded_by_str diff --git a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java index 239164dbd..595ae562e 100644 --- a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java +++ b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java @@ -34,7 +34,7 @@ public class GuardedByExample { private AutoCloseableReadWriteUpdateLock mReadWriteLock = new AutoCloseableReadWriteUpdateLock(); @GuardedBy("mLock") - Object f = new Object(); + private Object f = new Object(); @GuardedBy("this") Object g = new Object(); @@ -212,6 +212,16 @@ public class GuardedByExample { void synchronizedOnThisBad() { sGuardedByClass.toString(); } + + Object dontReportOnCompilerGenerated() { + return new Object() { + public void accessInAnonClassOk() { + synchronized (mLock) { + f.toString(); + } + } + }; + } // TODO: report on these cases /*