From: epriestley Date: Mon, 13 Jun 2011 15:58:01 +0000 (-0700) Subject: Add PHP lint check for expressions which can be statically evaluated X-Git-Tag: conduit-4~316 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=65c0d0793507dabc384d2ce1d951ee1250842b6a;p=arcanist.git Add PHP lint check for expressions which can be statically evaluated Summary: This is pretty coarse and could be refined, but I often do this when testing: lang=diff - if (some_complicated_condition()) + if (true || some_complicated_condition()) aran has caught me not only doing it but sending out diffs with it like 30 times. Catch it in lint instead. Test Plan: Unit test, added a "true || $junk" to the code and linted it. Reviewed By: aran Reviewers: aran, jungejason, tuomaspelkonen, pad CC: aran Differential Revision: 447 --- diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index 555ebe5e..303c54d3 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -172,25 +172,70 @@ class ArcanistXHPASTLinter extends ArcanistLinter { '>' => true, ); + static $logical = array( + '||' => true, + '&&' => true, + ); + foreach ($expressions as $expr) { $operator = $expr->getChildByIndex(1)->getConcreteString(); - if (empty($operators[$operator])) { - continue; + if (!empty($operators[$operator])) { + $left = $expr->getChildByIndex(0)->getSemanticString(); + $right = $expr->getChildByIndex(2)->getSemanticString(); + + if ($left == $right) { + $this->raiseLintAtNode( + $expr, + self::LINT_TAUTOLOGICAL_EXPRESSION, + 'Both sides of this expression are identical, so it always '. + 'evaluates to a constant.'); + } } - $left = $expr->getChildByIndex(0)->getSemanticString(); - $right = $expr->getChildByIndex(2)->getSemanticString(); + if (!empty($logical[$operator])) { + $left = $expr->getChildByIndex(0)->getSemanticString(); + $right = $expr->getChildByIndex(2)->getSemanticString(); - if ($left == $right) { - $this->raiseLintAtNode( - $expr, - self::LINT_TAUTOLOGICAL_EXPRESSION, - 'Both sides of this expression are identical, so it always '. - 'evaluates to a constant.'); + // NOTE: These will be null to indicate "could not evaluate". + $left = $this->evaluateStaticBoolean($left); + $right = $this->evaluateStaticBoolean($right); + + if (($operator == '||' && ($left === true || $right === true)) || + ($operator == '&&' && ($left === false || $right === false))) { + $this->raiseLintAtNode( + $expr, + self::LINT_TAUTOLOGICAL_EXPRESSION, + 'The logical value of this expression is static. Did you forget '. + 'to remove some debugging code?'); + } } } } + + /** + * Statically evaluate a boolean value from an XHP tree. + * + * TODO: Improve this and move it to XHPAST proper? + * + * @param string The "semantic string" of a single value. + * @return mixed ##true## or ##false## if the value could be evaluated + * statically; ##null## if static evaluation was not possible. + */ + private function evaluateStaticBoolean($string) { + switch (strtolower($string)) { + case '0': + case 'null': + case 'false': + return false; + case '1': + case 'true': + return true; + } + return null; + } + + protected function lintHashComments($root) { $tokens = $root->getTokens(); foreach ($tokens as $token) { diff --git a/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test b/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test index 4f8c3db2..d053a351 100644 --- a/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test @@ -15,7 +15,14 @@ if ($x == $y) { // See xhpast 0.54 -> 0.55. return $a->sub->value < $b->sub->value; +$skip_cache = true || $some_complicated_expression; +$skip_cache = $a || $b; +$skip_cache = false && something(); +$skip_cache = f(); + ~~~~~~~~~~ error:3:5 error:6:5 error:9:5 +error:18:15 +error:20:15