From f7903007eed26708b5590b77de2d34231637ec0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Tue, 24 Sep 2019 01:54:51 -0700 Subject: [PATCH] [required-props] Extend required prop setting with more custom suffixes Summary: Our annotation parameter parsing is too primitive to identify `resType` and before we only assumed that all Prop's can be set by any of the two suffixes: `Attr` and `Res`. After talking to Litho team, there is 3 more additions to these suffixes: `Dip`, `Sip`, and `Px`. Reviewed By: ngorogiannis Differential Revision: D17528482 fbshipit-source-id: 8d7f49130 --- infer/src/checkers/Litho.ml | 11 +++++--- .../java/litho/RequiredProps.java | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/infer/src/checkers/Litho.ml b/infer/src/checkers/Litho.ml index d59eb4fa3..a27d86a5b 100644 --- a/infer/src/checkers/Litho.ml +++ b/infer/src/checkers/Litho.ml @@ -140,13 +140,16 @@ module RequiredProps = struct && Procdesc.get_access proc_desc <> PredSymb.Private + let suffixes = String.Set.of_list ["Attr"; "Dip"; "Px"; "Res"; "Sp"] + let has_prop prop_set prop = String.Set.mem prop_set prop - (* @Prop(resType = ...) myProp can also be set via myProp(), myPropAttr(), or myPropRes(). + (* @Prop(resType = ...) myProp can also be set via myProp(), myPropAttr(), myPropDip(), myPropPx(), myPropRes() or myPropSp(). Our annotation parameter parsing is too primitive to identify resType, so just assume - that all @Prop's can be set any of these 3 ways. *) - || String.Set.mem prop_set (prop ^ "Attr") - || String.Set.mem prop_set (prop ^ "Res") + that all @Prop's can be set any of these 6 ways. *) + || String.Set.exists prop_set ~f:(fun el -> + String.chop_prefix el ~prefix:prop + |> Option.exists ~f:(fun suffix -> String.Set.mem suffixes suffix) ) let report astate tenv summary = diff --git a/infer/tests/codetoanalyze/java/litho/RequiredProps.java b/infer/tests/codetoanalyze/java/litho/RequiredProps.java index 74d1ef707..09efb32a1 100644 --- a/infer/tests/codetoanalyze/java/litho/RequiredProps.java +++ b/infer/tests/codetoanalyze/java/litho/RequiredProps.java @@ -129,6 +129,21 @@ class ResPropComponent extends Component { return this; } + public Builder propDip(Object o) { + this.mResPropComponent.prop = o; + return this; + } + + public Builder propPx(Object o) { + this.mResPropComponent.prop = o; + return this; + } + + public Builder propSp(Object o) { + this.mResPropComponent.prop = o; + return this; + } + public ResPropComponent build() { return mResPropComponent; } @@ -244,6 +259,18 @@ public class RequiredProps { mResPropComponent.create().propAttr(new Object()).build(); } + public void buildPropResWithDipOk() { + mResPropComponent.create().propDip(new Object()).build(); + } + + public void buildPropResWithPxOk() { + mResPropComponent.create().propPx(new Object()).build(); + } + + public void buildPropResWithSpOk() { + mResPropComponent.create().propSp(new Object()).build(); + } + public void buildPropResMissingBad() { mResPropComponent.create().build(); }