]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Warn about strstr() misuse
authorvrana <jakubv@fb.com>
Wed, 15 Aug 2012 20:30:40 +0000 (13:30 -0700)
committervrana <jakubv@fb.com>
Wed, 15 Aug 2012 20:44:41 +0000 (13:44 -0700)
Summary: See D3296#1.

Test Plan:
New test.
Linted Arcanist, libphutil, Phabricator repositories, found no false positive and one real positive.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D3297

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/slowness.lint-test

index 40569074841245258fc95f5edf70afc069849367..6779f839026098ca5f5809c8edb528eb95fd089d 100644 (file)
@@ -221,6 +221,49 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintPHP54Features($root);
     $this->lintPHT($root);
     $this->lintStrposUsedForStart($root);
+    $this->lintStrstrUsedForCheck($root);
+  }
+
+  public function lintStrstrUsedForCheck($root) {
+    $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
+    foreach ($expressions as $expression) {
+      $operator = $expression->getChildOfType(1, 'n_OPERATOR');
+      $operator = $operator->getConcreteString();
+
+      if ($operator != '===' && $operator != '!==') {
+        continue;
+      }
+
+      $false = $expression->getChildByIndex(0);
+      if ($false->getTypeName() == 'n_SYMBOL_NAME' &&
+          $false->getConcreteString() == 'false') {
+        $strstr = $expression->getChildByIndex(2);
+      } else {
+        $strstr = $false;
+        $false = $expression->getChildByIndex(2);
+        if ($false->getTypeName() != 'n_SYMBOL_NAME' ||
+            $false->getConcreteString() != 'false') {
+          continue;
+        }
+      }
+
+      if ($strstr->getTypeName() != 'n_FUNCTION_CALL') {
+        continue;
+      }
+
+      $name = strtolower($strstr->getChildByIndex(0)->getConcreteString());
+      if ($name == 'strstr' || $name == 'strchr') {
+        $this->raiseLintAtNode(
+          $strstr,
+          self::LINT_SLOWNESS,
+          "Use strpos() for checking if the string contains something.");
+      } else if ($name == 'stristr') {
+        $this->raiseLintAtNode(
+          $strstr,
+          self::LINT_SLOWNESS,
+          "Use stripos() for checking if the string contains something.");
+      }
+    }
   }
 
   public function lintStrposUsedForStart($root) {
index 60da4d8f39b5af577ddb13b29cdef88fdf7f61e9..6549eca3dbcdcf54898a25193cf38e27607da637 100644 (file)
@@ -4,7 +4,11 @@
 (strpos('a', 'b') !== 0);
 (0 === strpos('a', 'b'));
 (1 + 0 === strpos('a', 'b'));
+
+(strstr('a', 'b') === false);
+(strstr('a', 'b') === 'b');
 ~~~~~~~~~~
 warning:3:2
 warning:4:2
 warning:5:8
+warning:8:2