From fd19dfaebc74cc0f4927e1acaca7c82c961728f8 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 10 Mar 2011 15:11:41 -0800 Subject: [PATCH] Detect use of "+" on a string literal in PHP. Summary: This is realistically always wrong and the author means "." Test Plan: lint / unit Reviewed By: crackerjack Reviewers: crackerjack, aran CC: crackerjack Differential Revision: 68 --- .../linter/xhpast/ArcanistXHPASTLinter.php | 24 +++++++++++++++++++ .../data/wrong-concat-operator.lint-test | 9 +++++++ 2 files changed, 33 insertions(+) create mode 100644 src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index fd2f1c8f..d6aafab7 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -45,6 +45,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter { const LINT_COMMENT_STYLE = 18; const LINT_CLASS_FILENAME_MISMATCH = 19; const LINT_TAUTOLOGICAL_EXPRESSION = 20; + const LINT_PLUS_OPERATOR_ON_STRINGS = 21; public function getLintNameMap() { @@ -69,6 +70,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter { self::LINT_COMMENT_STYLE => 'Comment Style', self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch', self::LINT_TAUTOLOGICAL_EXPRESSION => 'Tautological Expression', + self::LINT_PLUS_OPERATOR_ON_STRINGS => 'Not String Concatenation', ); } @@ -146,6 +148,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter { $this->lintHashComments($root); $this->lintPrimaryDeclarationFilenameMatch($root); $this->lintTautologicalExpressions($root); + $this->lintPlusOperatorOnStrings($root); } private function lintTautologicalExpressions($root) { @@ -977,6 +980,27 @@ class ArcanistXHPASTLinter extends ArcanistLinter { ); } + private function lintPlusOperatorOnStrings($root) { + $binops = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); + foreach ($binops as $binop) { + $op = $binop->getChildByIndex(1); + if ($op->getConcreteString() != '+') { + continue; + } + + $left = $binop->getChildByIndex(0); + $right = $binop->getChildByIndex(2); + if (($left->getTypeName() == 'n_STRING_SCALAR') || + ($right->getTypeName() == 'n_STRING_SCALAR')) { + $this->raiseLintAtNode( + $binop, + self::LINT_PLUS_OPERATOR_ON_STRINGS, + "In PHP, '.' is the string concatenation operator, not '+'. This ". + "expression uses '+' with a string literal as an operand."); + } + } + } + protected function raiseLintAtToken( XHPASTToken $token, $code, diff --git a/src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test b/src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test new file mode 100644 index 00000000..aaf0de4d --- /dev/null +++ b/src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test @@ -0,0 +1,9 @@ +