[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
master
Dulma Churchill 7 years ago committed by Facebook Github Bot
parent 0b5c992a93
commit acbf6c90fe

@ -10,6 +10,8 @@
#import <Foundation/NSObject.h>
#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:) }];
};

@ -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, []

@ -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...`";
};

Loading…
Cancel
Save