]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Extend `LINT_BRACE_FORMATTING` to warn on missing braces
authorJoshua Spence <josh@joshuaspence.com>
Mon, 8 Dec 2014 12:30:32 +0000 (23:30 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 8 Dec 2014 12:30:33 +0000 (23:30 +1100)
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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/conditional-usage.lint-test
src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test
src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test

index ecad30f51a0f5033cd3ab99ecf7a1092b8ec2f49..ed7947543e594e0b6c1e78783d1a1dff5f5b15d9 100644 (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) {
index 2c98f060199e4f94961c3bd7c691bfaaa7e4033f..c04cd8920a5063534d382e58b463fba1193331f8 100644 (file)
@@ -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"}}
index 495f334af216ee3f8f2820643fcf9cf89d87a526..add2654f3399f220a37e91747406f226278e3c33 100644 (file)
@@ -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
 ~~~~~~~~~~
 <?php
 
@@ -57,3 +63,6 @@ try {}
 catch (Exception $x) {}
 
 function h() {}
+if ($x) foo();
+else bar();
+do baz(); while ($x);
index 5be41f20fb1ef922ec54325ee1117ec5956eeda9..6d0b43d4bf2a07fdcf5798021f30965e4b2befb1 100644 (file)
@@ -14,13 +14,14 @@ if  ($x) {}
 if
 
 ($x) {}
-if ($x)
+if ($x) {
   echo 100;
-else
+} else {
   echo 10;
-do
+}
+do {
   echo 1;
-while(true);
+} while(true);
 ~~~~~~~~~~
 warning:2:1
 warning:3:1
@@ -32,7 +33,7 @@ warning:7:6
 warning:8:1
 warning:13:3
 warning:14:3
-warning:23:1
+warning:24:3
 ~~~~~~~~~~
 <?php
 if ($x) {}
@@ -48,10 +49,11 @@ else if ($z) {}
 
 if ($x) {}
 if ($x) {}
-if ($x)
+if ($x) {
   echo 100;
-else
+} else {
   echo 10;
-do
+}
+do {
   echo 1;
-while (true);
+} while (true);