]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Tautological expression lint.
authorepriestley <epriestley@phabricator.com>
Sun, 6 Feb 2011 21:04:01 +0000 (13:04 -0800)
committerepriestley <epriestley@phabricator.com>
Sun, 6 Feb 2011 21:04:01 +0000 (13:04 -0800)
src/lint/linter/apachelicense/__init__.php
src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test [new file with mode: 0644]
src/workflow/export/ArcanistExportWorkflow.php

index f16cf6da4e923d56da9f88ee483bb990f051f1b9..08dd7e0e9db77c5fa47b210973d0edc7d91b4354 100644 (file)
@@ -6,7 +6,6 @@
 
 
 
-phutil_require_module('arcanist', 'exception/usage');
 phutil_require_module('arcanist', 'lint/linter/base');
 
 
index e1c292d01c7215df279748a83465016175630e4d..e9351731df906f9667e62c1ff5f17865302d3470 100644 (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',
     );
   }
 
@@ -108,7 +110,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
       }
     }
   }
-  
+
   public function getXHPASTTreeForPath($path) {
     return idx($this->trees, $path);
   }
@@ -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) {
diff --git a/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test b/src/lint/linter/xhpast/__tests__/data/tautological-expressions.lint-test
new file mode 100644 (file)
index 0000000..74ea7bc
--- /dev/null
@@ -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
index 5db19e93ac7f280979c769c422892a3c8dc3847d..b409a8604673d692d9fd96b6329e104ac0513d5e 100644 (file)
@@ -166,7 +166,7 @@ EOTEXT
       case self::SOURCE_LOCAL:
         $repository_api = $this->getRepositoryAPI();
         $parser = new ArcanistDiffParser();
-        
+
         if ($repository_api instanceof ArcanistGitAPI) {
           $this->parseGitRelativeCommit(
             $repository_api,