1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 14:52:40 +01:00

Extend LINT_BRACE_FORMATTING to warn on missing braces

Summary: Extend the `ArcanistXHPASTLinter::LINT_BRACE_FORMATTING` rule to raise a warning when `n_STATEMENT` is used without having `n_STATEMENT_LIST` as a parent. Essentially, this means that `if ($x) { do_y(); }` is preferred over `if ($x) do_y();`.

Test Plan: Added some test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D10571
This commit is contained in:
Joshua Spence 2014-12-08 23:30:32 +11:00
parent b46d4ed4ad
commit ee2070fadb
4 changed files with 33 additions and 9 deletions

View file

@ -890,6 +890,18 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
} }
} }
} }
$nodes = $root->selectDescendantsOfType('n_STATEMENT');
foreach ($nodes as $node) {
$parent = $node->getParentNode();
if ($parent && $parent->getTypeName() != 'n_STATEMENT_LIST') {
$this->raiseLintAtNode(
$node,
self::LINT_BRACE_FORMATTING,
'Use braces to surround a statement block.');
}
}
} }
private function lintTautologicalExpressions(XHPASTNode $root) { private function lintTautologicalExpressions(XHPASTNode $root) {

View file

@ -23,6 +23,7 @@ if ($var()) {}
error:5:3 error:5:3
error:7:3 error:7:3
error:12:7 error:12:7
warning:16:3
~~~~~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ ~~~~~~~~~~
{"config": {"xhpast.php-version": "5.3.0"}} {"config": {"xhpast.php-version": "5.3.0"}}

View file

@ -27,6 +27,9 @@ catch (Exception $x)
{} {}
function h(){} function h(){}
if ($x) foo();
else bar();
do baz(); while ($x);
~~~~~~~~~~ ~~~~~~~~~~
advice:3:14 advice:3:14
warning:7:13 warning:7:13
@ -38,6 +41,9 @@ warning:21:11
warning:24:4 warning:24:4
warning:26:21 warning:26:21
warning:29:13 warning:29:13
warning:30:9
warning:31:6
warning:32:4
~~~~~~~~~~ ~~~~~~~~~~
<?php <?php
@ -57,3 +63,6 @@ try {}
catch (Exception $x) {} catch (Exception $x) {}
function h() {} function h() {}
if ($x) foo();
else bar();
do baz(); while ($x);

View file

@ -14,13 +14,14 @@ if ($x) {}
if if
($x) {} ($x) {}
if ($x) if ($x) {
echo 100; echo 100;
else } else {
echo 10; echo 10;
do }
do {
echo 1; echo 1;
while(true); } while(true);
~~~~~~~~~~ ~~~~~~~~~~
warning:2:1 warning:2:1
warning:3:1 warning:3:1
@ -32,7 +33,7 @@ warning:7:6
warning:8:1 warning:8:1
warning:13:3 warning:13:3
warning:14:3 warning:14:3
warning:23:1 warning:24:3
~~~~~~~~~~ ~~~~~~~~~~
<?php <?php
if ($x) {} if ($x) {}
@ -48,10 +49,11 @@ else if ($z) {}
if ($x) {} if ($x) {}
if ($x) {} if ($x) {}
if ($x) if ($x) {
echo 100; echo 100;
else } else {
echo 10; echo 10;
do }
do {
echo 1; echo 1;
while (true); } while (true);