mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-28 17:52:42 +01:00
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
This commit is contained in:
parent
692d2a8b6f
commit
fd19dfaebc
2 changed files with 33 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
"a"."b";
|
||||
"a" + "b";
|
||||
"a" + $x;
|
||||
$x + $y + $z + "q" + 0;
|
||||
~~~~~~~~~~
|
||||
error:3:1
|
||||
error:4:1
|
||||
error:5:1
|
Loading…
Reference in a new issue