]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix another edge case for "function call should be type cast" linter rule
authorJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 03:13:11 +0000 (14:13 +1100)
committerJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 03:13:11 +0000 (14:13 +1100)
Summary: Fix a minor issue in which changing a function call to a type cast affects the result of an expression.

Test Plan: Added test case.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D14623

src/lint/linter/xhpast/rules/ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule.php
src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/binary-expression.lint-test [new file with mode: 0644]

index e39934accf04ddc45752858001696575cdac70db..9e4546c4cc5ebd52b8ec4c42c6ba324e3e72da04 100644 (file)
@@ -37,7 +37,13 @@ final class ArcanistFunctionCallShouldBeTypeCastXHPASTLinterRule
       // one parameter.
       if (count($parameters->getChildren()) == 1) {
         $parameter   = $parameters->getChildByIndex(0);
-        $replacement = '('.$cast_name.')'.$parameter->getConcreteString();
+        $replacement = '('.$cast_name.')';
+
+        if ($parameter->getTypeName() == 'n_BINARY_EXPRESSION') {
+          $replacement .= '('.$parameter->getConcreteString().')';
+        } else {
+          $replacement .= $parameter->getConcreteString();
+        }
       }
 
       if (strtolower($function_name) == 'intval') {
diff --git a/src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/binary-expression.lint-test b/src/lint/linter/xhpast/rules/__tests__/function-call-should-be-type-cast/binary-expression.lint-test
new file mode 100644 (file)
index 0000000..b3fbc1f
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+intval($x / 2) + 1;
+~~~~~~~~~~
+advice:2:1
+~~~~~~~~~~
+<?php
+(int)($x / 2) + 1;