[clang-tidy] misc-braces-around-statements.ShortStatementLines option
Add option ShortStatementLines to trigger this check only if the statement spans
over at least a given number of lines.
Modifications from the original patch:
merged test/clang-tidy/misc-braces-around-statements-always.cpp into
test/clang-tidy/misc-braces-around-statements.cpp and removed unnecessary
CHECK-NOTs from the tests.
http://reviews.llvm.org/D5642
Patch by Marek Kurdej!
llvm-svn: 219611
diff --git a/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-few-lines.cpp b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-few-lines.cpp
new file mode 100644
index 0000000..312b762
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-few-lines.cpp
@@ -0,0 +1,31 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 4}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
diff --git a/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-same-line.cpp b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-same-line.cpp
new file mode 100644
index 0000000..4f15112
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-same-line.cpp
@@ -0,0 +1,37 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 1}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+ // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if2")) {
+ // CHECK-FIXES: }
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-4]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if3") /*comment*/) {
+ // CHECK-FIXES: }
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
diff --git a/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-single-line.cpp b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-single-line.cpp
new file mode 100644
index 0000000..23b435f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements-single-line.cpp
@@ -0,0 +1,34 @@
+// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s misc-braces-around-statements %t -config="{CheckOptions: [{key: misc-braces-around-statements.ShortStatementLines, value: 2}]}" --
+// REQUIRES: shell
+
+void do_something(const char *) {}
+
+bool cond(const char *) {
+ return false;
+}
+
+void test() {
+ if (cond("if1") /*comment*/) do_something("same-line");
+
+ if (cond("if2"))
+ do_something("single-line");
+
+ if (cond("if3") /*comment*/)
+ // some comment
+ do_something("three"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-4]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if3") /*comment*/) {
+ // CHECK-FIXES: }
+
+ if (cond("if4") /*comment*/)
+ // some comment
+ do_something("many"
+ "many"
+ "many"
+ "many"
+ "lines");
+ // CHECK-MESSAGES: :[[@LINE-7]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if4") /*comment*/) {
+ // CHECK-FIXES: }
+}
diff --git a/clang-tools-extra/test/clang-tidy/misc-braces-around-statements.cpp b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements.cpp
index 44de6a0..bdfd6aa 100644
--- a/clang-tools-extra/test/clang-tidy/misc-braces-around-statements.cpp
+++ b/clang-tools-extra/test/clang-tidy/misc-braces-around-statements.cpp
@@ -11,8 +11,17 @@
#define EMPTY_MACRO_FUN()
void test() {
- // CHECK-NOT: warning
- // if
+ if (cond("if0") /*comment*/) do_something("same-line");
+ // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if0") /*comment*/) { do_something("same-line");
+ // CHECK-FIXES: }
+
+ if (cond("if0.1"))
+ do_something("single-line");
+ // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: statement should be inside braces
+ // CHECK-FIXES: if (cond("if0.1")) {
+ // CHECK-FIXES: }
+
if (cond("if1") /*comment*/)
// some comment
do_something("if1");
@@ -28,7 +37,6 @@
// CHECK-FIXES: if (cond("if3")) {
// CHECK-FIXES: }
- // if-else
if (cond("if-else1"))
do_something("if-else1");
// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: statement should be inside braces
@@ -44,8 +52,6 @@
do_something("if-else2 else");
}
- // CHECK-NOT: warning
- // if-else if-else
if (cond("if-else if-else1"))
do_something("if");
// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: statement should be inside braces
@@ -65,7 +71,6 @@
do_something("else");
}
- // CHECK-NOT: warning
for (;;)
do_something("for");
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces
@@ -80,7 +85,6 @@
// CHECK-FIXES: for (;;) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
int arr[4] = {1, 2, 3, 4};
for (int a : arr)
do_something("for-range");
@@ -96,7 +100,6 @@
// CHECK-FIXES: for (int a : arr) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
while (cond("while1"))
do_something("while");
// CHECK-MESSAGES: :[[@LINE-2]]:25: warning: statement should be inside braces
@@ -111,7 +114,6 @@
// CHECK-FIXES: while (false) {
// CHECK-FIXES: }
- // CHECK-NOT: warning
do
do_something("do1");
while (cond("do1"));
@@ -129,7 +131,6 @@
// CHECK-FIXES: do {
// CHECK-FIXES: } while (false);
- // CHECK-NOT: warning
if (cond("ifif1"))
// comment
if (cond("ifif2"))
@@ -159,7 +160,6 @@
// CHECK-FIXES: if (cond("ifif5")) {
// CHECK-FIXES: }/* multi-line
- // CHECK-NOT: warning
if (1) while (2) if (3) for (;;) do ; while(false) /**/;/**/
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should be inside braces
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces
@@ -171,8 +171,4 @@
// CHECK-FIXES-NEXT: }
// CHECK-FIXES-NEXT: }
// CHECK-FIXES-NEXT: }
-
- // CHECK-NOT: warning
-
- // CHECK-MESSAGES: {{clang-tidy applied [0-9]+ of [0-9]+ suggested fixes.}}
}