1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-25 08:12:40 +01:00

Tautological expression lint.

This commit is contained in:
epriestley 2011-02-06 13:04:01 -08:00
parent db9f629499
commit 4b51720ba1
4 changed files with 58 additions and 3 deletions

View file

@ -6,7 +6,6 @@
phutil_require_module('arcanist', 'exception/usage');
phutil_require_module('arcanist', 'lint/linter/base');

View file

@ -39,6 +39,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
const LINT_EXIT_EXPRESSION = 17;
const LINT_COMMENT_STYLE = 18;
const LINT_CLASS_FILENAME_MISMATCH = 19;
const LINT_TAUTOLOGICAL_EXPRESSION = 20;
public function getLintNameMap() {
@ -62,6 +63,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression',
self::LINT_COMMENT_STYLE => 'Comment Style',
self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch',
self::LINT_TAUTOLOGICAL_EXPRESSION => 'Tautological Expression',
);
}
@ -136,6 +138,42 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
$this->lintArrayIndexWhitespace($root);
$this->lintHashComments($root);
$this->lintPrimaryDeclarationFilenameMatch($root);
$this->lintTautologicalExpressions($root);
}
private function lintTautologicalExpressions($root) {
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
static $operators = array(
'-' => true,
'/' => true,
'-=' => true,
'/=' => true,
'<=' => true,
'<' => true,
'==' => true,
'===' => true,
'>=' => true,
'>' => true,
);
foreach ($expressions as $expr) {
$operator = $expr->getChildByIndex(1)->getConcreteString();
if (empty($operators[$operator])) {
continue;
}
$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.');
}
}
}
protected function lintHashComments($root) {

View file

@ -0,0 +1,18 @@
<?php
if ($x == $x) {
}
if ($x->m(3) < $x->m(3)) {
}
if ($y[2] - $y[2]) {
}
if ($x == $y) {
}
~~~~~~~~~~
error:3:5
error:6:5
error:9:5