[clang-tools-extra] [clang-tidy] Add new check 'bugprone-inconsistent-ifelse-braces' (PR #162361)
Davide Cunial via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 19 12:26:20 PST 2025
================
@@ -0,0 +1,35 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s readability-inconsistent-ifelse-braces %t
+
+void do_something(const char *) {}
+
+// Positive tests.
+void f(bool b) {
+ if (b) [[likely]] do_something("if-same-line");
+ else {
+ }
+ // CHECK-MESSAGES: :[[@LINE-3]]:9: warning: <message> [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: if (b) { {{[[][[]}}likely{{[]][]]}} do_something("if-same-line");
+ // CHECK-FIXES: } else {
----------------
capitan-davide wrote:
I was adding some tests with likely/unlikely attributes but this fix-it seems wrong to me (which is the same applied by `readability-braces-around-statements`). From:
```c++
if (b) [[likely]] do_something("if-same-line");
else {
}
```
To:
```c++
if (b) { [[likely]] do_something("if-same-line");
} else {
}
```
Also [cppreference](https://en.cppreference.com/w/cpp/language/if.html) is not clear on where these attributes should be placed.
What do you think? Are these semanically equivalent?
```c++
[[likely]] if (b) do_something(); // (A)
if (b) [[likely]] do_something(); // (B)
if (b) { [[likely]] do_something(); } // (C)
```
https://github.com/llvm/llvm-project/pull/162361
More information about the cfe-commits
mailing list