From df1063b9eb396ebf40c2035fe95ba648bef8701e Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Tue, 22 Aug 2017 07:37:36 -0700 Subject: [PATCH] [linters] Adding example of checking a parameter for empty struct Reviewed By: jvillard Differential Revision: D5678330 fbshipit-source-id: 3d1891f --- .../TestParamterLabelsChecks.mm | 1 + .../objcpp/linters-for-test-only/issues.exp | 9 +++++---- .../linters-for-test-only/linters_example.al | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm index d38096964..8ca291856 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm @@ -25,6 +25,7 @@ struct SomeStruct { SomeButton* buttonComponent(void); SomeButton* buttonComponent(void) { + // flagging passing empty struct return [SomeButton newWithStruct:{} map:{} object:nil number:0]; }; diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp index f5090a1d3..84f328065 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp @@ -4,7 +4,8 @@ codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWi codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithActionRef:, 40, TEST_REFERENCE, [] codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithConstAction:, 21, TEST_REFERENCE, [] codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWithConstAction:, 36, TEST_REFERENCE, [] -codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 33, TEST_PARAMETER_LABEL, [] -codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 33, TEST_PARAMETER_LABEL_REGEXP, [] -codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 28, TEST_PARAMETER_LABEL, [] -codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 28, TEST_PARAMETER_LABEL_REGEXP, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 34, TEST_PARAMETER_LABEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 34, TEST_PARAMETER_LABEL_REGEXP, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 29, TEST_PARAMETER_LABEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 29, TEST_PARAMETER_LABEL_EMPTY_STRUCT, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 29, TEST_PARAMETER_LABEL_REGEXP, [] diff --git a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al index d3f291b3c..5f9b11ec9 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/linters_example.al @@ -35,3 +35,23 @@ DEFINE-CHECKER TEST_PARAMETER_LABEL_REGEXP = { HOLDS-IN-NODE ObjCMessageExpr; SET message = "Found method with parameter labeled with `number` and with type `int`"; }; + +DEFINE-CHECKER TEST_PARAMETER_LABEL_EMPTY_STRUCT = { + LET is_empty_init_list = + WHEN ((is_node("ImplicitValueInitExpr")) HOLDS-EVERYWHERE-NEXT) + HOLDS-IN-NODE InitListExpr; + LET is_empty_struct = + WHEN ((is_empty_init_list) HOLDS-EVERYWHERE-NEXT) + HOLDS-IN-NODE CXXBindTemporaryExpr; + LET method_has_parameter_type = + WHEN + HOLDS-NEXT WITH-TRANSITION ParameterName "newWithStruct" + (is_empty_struct) + HOLDS-IN-NODE ObjCMessageExpr; + SET report_when = + WHEN + method_has_parameter_type + AND call_method(REGEXP("^new.*:$")) + HOLDS-IN-NODE ObjCMessageExpr; + SET message = "Do not pass empty struct to the parameter `newWithStruct` of method `new...`"; +};