diff --git a/infer/src/backend/rearrange.ml b/infer/src/backend/rearrange.ml index cde56f2f9..7165d2a2c 100644 --- a/infer/src/backend/rearrange.ml +++ b/infer/src/backend/rearrange.ml @@ -617,7 +617,14 @@ let prop_iter_add_hpred_footprint_to_prop pname tenv prop (lexp, typ) inst = let add_guarded_by_constraints prop lexp pdesc = let pname = Cfg.Procdesc.get_proc_name pdesc in let excluded_guardedby_string str = - str = "ui_thread" in (* don't warn on @GuardedBy("ui_thread") *) + (* nothing with a space in it can be a valid Java expression, shouldn't warn *) + let is_invalid_exp_str str = + String.contains str ' ' in + (* don't warn on @GuardedBy("ui_thread") in any form *) + let is_ui_thread str = + let lowercase_str = String.lowercase str in + lowercase_str = "ui_thread" || lowercase_str = "ui-thread" || lowercase_str = "uithread" in + is_invalid_exp_str str || is_ui_thread str in let guarded_by_str_is_this guarded_by_str = string_is_suffix "this" guarded_by_str in let guarded_by_str_is_class guarded_by_str class_str = diff --git a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java index 83da3e419..b79bde7fb 100644 --- a/infer/tests/codetoanalyze/java/infer/GuardedByExample.java +++ b/infer/tests/codetoanalyze/java/infer/GuardedByExample.java @@ -49,9 +49,6 @@ public class GuardedByExample { @GuardedBy("mReadWriteLock") Object i = new Object(); - @GuardedBy("ui_thread") - Object t = new Object(); - private static Object sLock = new Object(); @GuardedBy("sLock") @@ -183,10 +180,6 @@ public class GuardedByExample { } } - void readTok() { - this.t.toString(); - } - void readWriteLockOk() { try (AutoCloseableReadWriteUpdateLock lock = mReadWriteLock) { this.i.toString(); @@ -293,6 +286,29 @@ public class GuardedByExample { } + @GuardedBy("ui_thread") + Object uiThread1; + @GuardedBy("ui-thread") + Object uiThread2; + @GuardedBy("uithread") + Object uiThread3; + + @GuardedBy("something that's clearly not an expression") + Object nonExpression; + + // tests for not reporting false alarms on unrecognized GuardedBy strings + void accessUnrecognizedGuardedByFieldsOk() { + uiThread1 = new Object(); + uiThread1.toString(); + uiThread2 = new Object(); + uiThread2.toString(); + uiThread3 = new Object(); + uiThread3.toString(); + nonExpression = new Object(); + nonExpression.toString(); + } + + // TODO: report on these cases /* public void unguardedCallSiteBad1() {