Author: Berkay Sahin Date: 2026-03-08T13:58:03-07:00 New Revision: ae282974b8a40e20a4c55dfffba84d24db843d74
URL: https://github.com/llvm/llvm-project/commit/ae282974b8a40e20a4c55dfffba84d24db843d74 DIFF: https://github.com/llvm/llvm-project/commit/ae282974b8a40e20a4c55dfffba84d24db843d74.diff LOG: [clang-tidy] Fix readability-else-after-return for [[likely]]/[[unlikely]] if (#184684) Following the PR #181878 I have noticed a false negative when if is attributed. Repro: ```cpp void f() { if (true) { return; } else { // Warns as expected } if (true) [[likely]] { return; } else { // False-negative } } ``` Godbolt: https://godbolt.org/z/cvGdovjhx Added: clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-cxx20.cpp Modified: clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp index 7e93d619e2a9f..74221163f3efa 100644 --- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp @@ -178,8 +178,9 @@ void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) { const auto IfWithInterruptingThenElse = ifStmt(unless(isConstexpr()), unless(isConsteval()), - hasThen(stmt(anyOf(InterruptsControlFlow, - compoundStmt(has(InterruptsControlFlow))))), + hasThen(stripLabelLikeStatements( + stmt(anyOf(InterruptsControlFlow, + compoundStmt(has(InterruptsControlFlow)))))), hasElse(stmt().bind("else"))) .bind("if"); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 9f4d7e6923fa0..6e1a116a30bf8 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -299,8 +299,13 @@ Changes in existing checks when a member expression has a non-identifier name. - Improved :doc:`readability-else-after-return - <clang-tidy/checks/readability/else-after-return>` check by fixing missed - diagnostics when ``if`` statements appear in unbraced ``switch`` case labels. + <clang-tidy/checks/readability/else-after-return>` check: + + - Fixed missed diagnostics when ``if`` statements appear in unbraced + ``switch`` case labels. + + - Added support for handling attributed ``if`` then-branches such as + ``[[likely]]`` and ``[[unlikely]]``. - Improved :doc:`readability-enum-initial-value <clang-tidy/checks/readability/enum-initial-value>` check: the warning message diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-cxx20.cpp new file mode 100644 index 0000000000000..589d2b6e2bb68 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-cxx20.cpp @@ -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 + int _ = 10; + + if (false) + return; + else [[likely]] { // comment-3 + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return' + // CHECK-FIXES: {{^}} {{[[][[]}}likely{{[]][]]}} { // comment-3 + int _ = 31; + } + + if (true) + return; + else [[unlikely]] // comment-4 + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return' + // CHECK-FIXES: {{^}} {{[[][[]}}unlikely{{[]][]]}} // comment-4 + g(); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
