Skip to content

Commit 540a43c

Browse files
[clang-tidy] Add some test for constexpr if
1 parent 6a652cd commit 540a43c

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %check_clang_tidy -std=c++23-or-later %s bugprone-inconsistent-ifelse-braces %t
22

33
bool cond(const char *) { return false; }
4-
54
void do_something(const char *) {}
65

76
// Positive tests.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-inconsistent-ifelse-braces %t
2+
3+
constexpr bool cond(const char *) { return false; }
4+
constexpr void do_something(const char *) {}
5+
6+
// Positive tests.
7+
void f() {
8+
if constexpr (cond("if0") /*comment*/) do_something("if-same-line");
9+
else {
10+
}
11+
// CHECK-MESSAGES: :[[@LINE-3]]:41: warning: <message> [bugprone-inconsistent-ifelse-braces]
12+
// CHECK-FIXES: if constexpr (cond("if0") /*comment*/) { do_something("if-same-line");
13+
// CHECK-FIXES: } else {
14+
15+
if constexpr (cond("if0.1") /*comment*/) {
16+
} else do_something("else-same-line");
17+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces]
18+
// CHECK-FIXES: } else { do_something("else-same-line");
19+
// CHECK-FIXES: }
20+
21+
if constexpr (cond("if1"))
22+
do_something("if-single-line");
23+
else {
24+
}
25+
// CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces]
26+
// CHECK-FIXES: if constexpr (cond("if1")) {
27+
// CHECK-FIXES: } else {
28+
29+
if constexpr (cond("if1.1")) {
30+
} else
31+
do_something("else-single-line");
32+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces]
33+
// CHECK-FIXES: } else {
34+
// CHECK-FIXES: }
35+
36+
if constexpr (cond("if2") /*comment*/)
37+
// some comment
38+
do_something("if-multi-line");
39+
else {
40+
}
41+
// CHECK-MESSAGES: :[[@LINE-5]]:41: warning: <message> [bugprone-inconsistent-ifelse-braces]
42+
// CHECK-FIXES: if constexpr (cond("if2") /*comment*/) {
43+
// CHECK-FIXES: } else {
44+
45+
if constexpr (cond("if2.1") /*comment*/) {
46+
} else
47+
// some comment
48+
do_something("else-multi-line");
49+
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces]
50+
// CHECK-FIXES: } else {
51+
// CHECK-FIXES: }
52+
53+
if constexpr (cond("if3")) do_something("elseif-same-line");
54+
else if constexpr (cond("if3")) {
55+
} else {
56+
}
57+
// CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces]
58+
// CHECK-FIXES: if constexpr (cond("if3")) { do_something("elseif-same-line");
59+
// CHECK-FIXES: } else if constexpr (cond("if3")) {
60+
61+
if constexpr (cond("if3.1")) {
62+
} else if constexpr (cond("if3.1")) do_something("elseif-same-line");
63+
else {
64+
}
65+
// CHECK-MESSAGES: :[[@LINE-3]]:38: warning: <message> [bugprone-inconsistent-ifelse-braces]
66+
// CHECK-FIXES: } else if constexpr (cond("if3.1")) { do_something("elseif-same-line");
67+
// CHECK-FIXES: } else {
68+
69+
if constexpr (cond("if3.2")) {
70+
} else if constexpr (cond("if3.2")) {
71+
} else do_something("else-same-line");
72+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces]
73+
// CHECK-FIXES: } else { do_something("else-same-line");
74+
// CHECK-FIXES: }
75+
76+
if constexpr (cond("if4-outer"))
77+
if constexpr (cond("if4-inner"))
78+
do_something("if-single-line");
79+
else {
80+
}
81+
else {
82+
}
83+
// CHECK-MESSAGES: :[[@LINE-7]]:35: warning: <message> [bugprone-inconsistent-ifelse-braces]
84+
// CHECK-MESSAGES: :[[@LINE-7]]:37: warning: <message> [bugprone-inconsistent-ifelse-braces]
85+
// CHECK-FIXES: if constexpr (cond("if4-outer")) {
86+
// CHECK-FIXES: if constexpr (cond("if4-inner")) {
87+
// CHECK-FIXES: } else {
88+
// CHECK-FIXES: } else {
89+
90+
if constexpr (cond("if5"))
91+
do_something("if-single-line");
92+
else if constexpr (cond("if5")) {
93+
}
94+
// CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces]
95+
// CHECK-FIXES: if constexpr (cond("if5")) {
96+
// CHECK-FIXES: } else if constexpr (cond("if5")) {
97+
98+
if constexpr (cond("if5.1")) {
99+
} else if constexpr (cond("if5.1"))
100+
do_something("elseif-single-line");
101+
// CHECK-MESSAGES: :[[@LINE-2]]:38: warning: <message> [bugprone-inconsistent-ifelse-braces]
102+
// CHECK-FIXES: } else if constexpr (cond("if5.1")) {
103+
// CHECK-FIXES: }
104+
}
105+
106+
// Negative tests.
107+
void g() {
108+
if constexpr (cond("if0")) {
109+
do_something("if-single-line");
110+
} else if constexpr (cond("if0")) {
111+
do_something("elseif-single-line");
112+
} else {
113+
do_something("else-single-line");
114+
}
115+
116+
if constexpr (cond("if1"))
117+
do_something("if-single-line");
118+
else if constexpr (cond("if1"))
119+
do_something("elseif-single-line");
120+
else
121+
do_something("else-single-line");
122+
}

clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %check_clang_tidy -std=c++98-or-later %s bugprone-inconsistent-ifelse-braces %t
22

33
bool cond(const char *) { return false; }
4-
54
void do_something(const char *) {}
65

76
// Positive tests.

0 commit comments

Comments
 (0)