[clang-tools-extra] 2a114d1 - [clang-tidy] Ignore `if consteval` in else-after-return (#91588)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 13 10:12:34 PDT 2024


Author: Jover
Date: 2024-05-13T19:12:29+02:00
New Revision: 2a114d171d4c1e6c742d039a289bf509b86668ac

URL: https://github.com/llvm/llvm-project/commit/2a114d171d4c1e6c742d039a289bf509b86668ac
DIFF: https://github.com/llvm/llvm-project/commit/2a114d171d4c1e6c742d039a289bf509b86668ac.diff

LOG: [clang-tidy] Ignore `if consteval` in else-after-return (#91588)

Added: 
    clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-if-consteval.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 1e85caf688355..2b185e7594add 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -113,7 +113,7 @@ static bool containsDeclInScope(const Stmt *Node) {
 }
 
 static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,
-                           const Stmt *Else, SourceLocation ElseLoc) {
+                                  const Stmt *Else, SourceLocation ElseLoc) {
   auto Remap = [&](SourceLocation Loc) {
     return Context.getSourceManager().getExpansionLoc(Loc);
   };
@@ -172,7 +172,7 @@ void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
       breakStmt().bind(InterruptingStr), cxxThrowExpr().bind(InterruptingStr)));
   Finder->addMatcher(
       compoundStmt(
-          forEach(ifStmt(unless(isConstexpr()),
+          forEach(ifStmt(unless(isConstexpr()), unless(isConsteval()),
                          hasThen(stmt(
                              anyOf(InterruptsControlFlow,
                                    compoundStmt(has(InterruptsControlFlow))))),

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fc976ce3a33d5..8183d394cf425 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -339,6 +339,10 @@ Changes in existing checks
   <clang-tidy/checks/readability/duplicate-include>` check by excluding include
   directives that form the filename using macro.
 
+- Improved :doc:`readability-else-after-return
+  <clang-tidy/checks/readability/else-after-return>` check to ignore
+  `if consteval` statements, for which the `else` branch must not be removed.
+
 - Improved :doc:`readability-identifier-naming
   <clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
   mode by resolving symbolic links to header files. Fixed handling of Hungarian

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-if-consteval.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-if-consteval.cpp
new file mode 100644
index 0000000000000..8810d215ee97f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-if-consteval.cpp
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy -std=c++23 %s readability-else-after-return %t 
+
+// Consteval if is an exception to the rule, we cannot remove the else.
+void f() {
+  if (sizeof(int) > 4) {
+    return;
+  } else {
+    return;
+  }
+  // CHECK-MESSAGES: [[@LINE-3]]:5: warning: do not use 'else' after 'return'
+
+  if consteval {
+    return;
+  } else {
+    return;
+  }
+}


        


More information about the cfe-commits mailing list