From: Joshua Spence Date: Mon, 8 Dec 2014 12:30:32 +0000 (+1100) Subject: Extend `LINT_BRACE_FORMATTING` to warn on missing braces X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ee2070fadbc50d74d0f47071c436330c46ac4e10;p=arcanist.git 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 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index ecad30f5..ed794754 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -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) { diff --git a/src/lint/linter/__tests__/xhpast/conditional-usage.lint-test b/src/lint/linter/__tests__/xhpast/conditional-usage.lint-test index 2c98f060..c04cd892 100644 --- a/src/lint/linter/__tests__/xhpast/conditional-usage.lint-test +++ b/src/lint/linter/__tests__/xhpast/conditional-usage.lint-test @@ -23,6 +23,7 @@ if ($var()) {} error:5:3 error:7:3 error:12:7 +warning:16:3 ~~~~~~~~~~ ~~~~~~~~~~ {"config": {"xhpast.php-version": "5.3.0"}} diff --git a/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test b/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test index 495f334a..add2654f 100644 --- a/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test +++ b/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test @@ -27,6 +27,9 @@ catch (Exception $x) {} function h(){} +if ($x) foo(); +else bar(); +do baz(); while ($x); ~~~~~~~~~~ advice:3:14 warning:7:13 @@ -38,6 +41,9 @@ warning:21:11 warning:24:4 warning:26:21 warning:29:13 +warning:30:9 +warning:31:6 +warning:32:4 ~~~~~~~~~~