[clang-tools-extra] [clang-tidy] Fix readability-else-after-return for [[likely]]/[[unlikely]] if (PR #184684)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 6 22:10:13 PST 2026
================
@@ -0,0 +1,41 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s readability-else-after-return %t
+
+void g();
+
+void f() {
+ if (true) [[likely]] {
+ return;
+ } else { // comment-0
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+ // CHECK-FIXES: {{^}} } // comment-0
+ }
+
+ if (false) [[unlikely]] {
+ return;
+ } else { // comment-1
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+ // CHECK-FIXES: {{^}} } // comment-1
+ }
+
+ if (true) [[likely]]
+ return;
+ else // comment-2
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return'
+ // CHECK-FIXES: {{^}} else // comment-2
----------------
localspook wrote:
The check is being conservative because of the declaration in the `else` branch. There's an option to control this:
https://github.com/llvm/llvm-project/blob/babead29c3fb2c44b85e8fd2f269ee56b69cafb2/clang-tools-extra/docs/clang-tidy/checks/readability/else-after-return.rst?plain=1#L66-L71
https://github.com/llvm/llvm-project/pull/184684
More information about the cfe-commits
mailing list