From acbf6c90fe7514ed9092e7c84e53de5f40dc9b62 Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Fri, 25 Aug 2017 02:58:46 -0700 Subject: [PATCH] [linters] Add example of checking whether a parameter passed to a function is initialized with a selector only Reviewed By: dshahidehpour Differential Revision: D5688786 fbshipit-source-id: c4c71ba --- .../TestParamterLabelsChecks.mm | 19 +++++++++++++++ .../objcpp/linters-for-test-only/issues.exp | 14 ++++++----- .../linters-for-test-only/linters_example.al | 23 +++++++++++++++++++ 3 files changed, 50 insertions(+), 6 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 b9b7e896e..2efd639c7 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm @@ -10,6 +10,8 @@ #import +#pragma GCC diagnostic ignored "-Wundeclared-selector" + struct SomeStruct { NSString* someLabel; }; @@ -38,3 +40,20 @@ SomeButton* anotherButtonComponent(void) { object:@"a string object" number:5]; }; + +struct CKComponentAction { + CKComponentAction(SEL selector); + CKComponentAction(id target, SEL selector); +}; + +@interface FBSomeComponent : NSObject ++ (instancetype)newWithAction:(CKComponentAction)action; +@end + +void foo(NSObject* someObject) { + [FBSomeComponent newWithAction:@selector(thisBad:)]; + + [FBSomeComponent newWithAction:{ @selector(thisIsAlsoBad:) }]; + + [FBSomeComponent newWithAction:{ someObject, @selector(thisIsGood:) }]; +}; 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 531779d03..f55f73fca 100644 --- a/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp +++ b/infer/tests/codetoanalyze/objcpp/linters-for-test-only/issues.exp @@ -4,9 +4,11 @@ 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, 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, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 36, TEST_PARAMETER_LABEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, anotherButtonComponent, 36, TEST_PARAMETER_LABEL_REGEXP, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 31, TEST_PARAMETER_LABEL, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 31, TEST_PARAMETER_LABEL_EMPTY_MAP, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 31, TEST_PARAMETER_LABEL_EMPTY_STRUCT, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, buttonComponent, 31, TEST_PARAMETER_LABEL_REGEXP, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, foo, 54, TEST_PARAMETER_SELECTOR, [] +codetoanalyze/objcpp/linters-for-test-only/TestParamterLabelsChecks.mm, foo, 56, TEST_PARAMETER_SELECTOR, [] 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 136d1a8fc..67f464edf 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 @@ -79,3 +79,26 @@ DEFINE-CHECKER TEST_PARAMETER_LABEL_EMPTY_MAP = { HOLDS-IN-NODE ObjCMessageExpr; SET message = "Do not pass empty map to the parameter `map` of method `new...`"; }; + +DEFINE-CHECKER TEST_PARAMETER_SELECTOR = { + + LET initialized_with_selector_expr = + WHEN (is_node("ObjCSelectorExpr") HOLDS-EVERYWHERE-NEXT) HOLDS-IN-NODE CXXConstructExpr; + + LET materialized_with_selector_expr = + WHEN (initialized_with_selector_expr HOLDS-EVENTUALLY) HOLDS-IN-NODE CXXConstructExpr; + + LET method_has_parameter_type = + WHEN + HOLDS-NEXT WITH-TRANSITION ParameterName "newWithAction" + (initialized_with_selector_expr OR + materialized_with_selector_expr) + 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 construct the Component action with a selector only...`"; +};