From: Joshua Spence Date: Mon, 23 Nov 2015 22:49:43 +0000 (+1100) Subject: Improve the "unexpected return value" linter rule X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9e78d15fc0c7182f0ef0e0c9795f11a6ac7a50f0;p=arcanist.git Improve the "unexpected return value" linter rule Summary: Return values within constructors are acceptable if they are within a closure or anonymous function. Test Plan: Added a test case. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D14564 --- diff --git a/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php index b73ea0a5..10ffda44 100644 --- a/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php @@ -33,6 +33,10 @@ final class ArcanistUnexpectedReturnValueXHPASTLinterRule continue; } + if ($this->isInAnonymousFunction($return)) { + continue; + } + $this->raiseLintAtNode( $return, pht( @@ -45,4 +49,15 @@ final class ArcanistUnexpectedReturnValueXHPASTLinterRule } } + private function isInAnonymousFunction(XHPASTNode $node) { + while ($node) { + if ($node->getTypeName() == 'n_FUNCTION_DECLARATION' && + $node->getChildByIndex(2)->getTypeName() == 'n_EMPTY') { + return true; + } + + $node = $node->getParentNode(); + } + } + } diff --git a/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test new file mode 100644 index 00000000..65536bfe --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test @@ -0,0 +1,11 @@ +closure = function() { + return null; + }; + } +} +~~~~~~~~~~