From 0b5c992a93c2ea38cb6fe9a0c39e2427694c88db Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Fri, 25 Aug 2017 02:45:29 -0700 Subject: [PATCH] [linters] Add an example of checking for an empty map in the argument of a method call Reviewed By: ikenwoo Differential Revision: D5678779 fbshipit-source-id: 90ecb0d --- .../TestParamterLabelsChecks.mm | 2 +- .../objcpp/linters-for-test-only/issues.exp | 1 + .../linters-for-test-only/linters_example.al | 24 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) 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 8ca291856..b9b7e896e 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm @@ -25,7 +25,7 @@ struct SomeStruct { SomeButton* buttonComponent(void); SomeButton* buttonComponent(void) { - // flagging passing empty struct + // flagging passing empty struct and map 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 84f328065..531779d03 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp @@ -7,5 +7,6 @@ codetoanalyze/objcpp/linters-for-test-only/ReferenceTest.mm, ReferenceTest_newWi 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_MAP, [] 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 5f9b11ec9..136d1a8fc 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 @@ -55,3 +55,27 @@ DEFINE-CHECKER TEST_PARAMETER_LABEL_EMPTY_STRUCT = { HOLDS-IN-NODE ObjCMessageExpr; SET message = "Do not pass empty struct to the parameter `newWithStruct` of method `new...`"; }; + +DEFINE-CHECKER TEST_PARAMETER_LABEL_EMPTY_MAP = { + //This means the node has no children. + LET constructor_with_no_parameters = + WHEN (FALSE HOLDS-EVERYWHERE-NEXT) HOLDS-IN-NODE CXXConstructExpr; + + LET temp_expr = + WHEN (constructor_with_no_parameters HOLDS-EVERYWHERE-NEXT) HOLDS-IN-NODE CXXBindTemporaryExpr; + + LET is_empty_map = + WHEN (temp_expr HOLDS-EVERYWHERE-NEXT) HOLDS-IN-NODE MaterializeTemporaryExpr; + + LET method_has_parameter_type = + WHEN + HOLDS-NEXT WITH-TRANSITION ParameterName "map" + (is_empty_map) + 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 map to the parameter `map` of method `new...`"; +};