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

via cfe-commits cfe-commits at lists.llvm.org
Sun May 12 19:09:26 PDT 2024


https://github.com/JoverZhang updated https://github.com/llvm/llvm-project/pull/91588

>From 19bf94ca3c093a6904482eab599f8366cad1384e Mon Sep 17 00:00:00 2001
From: Jover Zhang <joverzh at gmail.com>
Date: Thu, 9 May 2024 20:56:51 +0800
Subject: [PATCH 1/3] [clang-tidy] Ignore `if consteval` in else-after-return

---
 .../clang-tidy/readability/ElseAfterReturnCheck.cpp           | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 1e85caf688355..3ee09b2e6442c 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -317,6 +317,10 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
     return;
   }
 
+  if (If->isConsteval()) {
+    return;
+  }
+
   DiagnosticBuilder Diag = diag(ElseLoc, WarningMessage)
                            << ControlFlowInterruptor << SourceRange(ElseLoc);
   removeElseAndBrackets(Diag, *Result.Context, Else, ElseLoc);

>From 2a394ddde92b23f9407ca74e597b569bb01da35f Mon Sep 17 00:00:00 2001
From: Jover Zhang <joverzh at gmail.com>
Date: Sun, 12 May 2024 22:40:14 +0800
Subject: [PATCH 2/3] Add test case

---
 .../readability/ElseAfterReturnCheck.cpp        |  8 ++------
 .../else-after-return-if-consteval.cpp          | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 6 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/else-after-return-if-consteval.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 3ee09b2e6442c..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))))),
@@ -317,10 +317,6 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
     return;
   }
 
-  if (If->isConsteval()) {
-    return;
-  }
-
   DiagnosticBuilder Diag = diag(ElseLoc, WarningMessage)
                            << ControlFlowInterruptor << SourceRange(ElseLoc);
   removeElseAndBrackets(Diag, *Result.Context, Else, ElseLoc);
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..6235d8623ca7b
--- /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++20 %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;
+  }
+}

>From 1e88302ea84491967ad61e6c32ad87c15e02f3bf Mon Sep 17 00:00:00 2001
From: Jover Zhang <joverzh at gmail.com>
Date: Mon, 13 May 2024 10:09:04 +0800
Subject: [PATCH 3/3] Add a release note

---
 clang-tools-extra/docs/ReleaseNotes.rst                       | 4 ++++
 .../checkers/readability/else-after-return-if-consteval.cpp   | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6a2b8d3b6ded6..aba5102b32efb 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -337,6 +337,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
+  `consteval if` condition, where need to retain the else statement.
+
 - 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
index 6235d8623ca7b..8810d215ee97f 100644
--- 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
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++20 %s readability-else-after-return %t 
+// 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() {



More information about the cfe-commits mailing list