[PATCH] D158480: [clang-tidy][readability-braces-around-statements] ignore false-positive for constexpr if statement in lambda expression

Congcong Cai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 21 23:21:15 PDT 2023


HerrCai0907 updated this revision to Diff 552227.
HerrCai0907 added a comment.

update release note


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158480/new/

https://reviews.llvm.org/D158480

Files:
  clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp
@@ -457,3 +457,25 @@
   // CHECK-FIXES-NEXT: }
 
 }
+
+template <bool A>
+auto constexpr_lambda_1 = [] {
+  if constexpr (A) {
+    1;
+  }
+};
+
+template <bool A>
+auto constexpr_lambda_2 = [] {
+  // CHECK-MESSAGES: :[[@LINE+1]]:19: warning: statement should be inside braces
+  if constexpr (A)
+    1;
+  // CHECK-FIXES:if constexpr (A) {
+  // CHECK-FIXES-NEXT:1;
+  // CHECK-FIXES-NEXT:}
+};
+
+void test_constexpr() {
+  constexpr_lambda_1<false>();
+  constexpr_lambda_2<false>();
+}
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -228,6 +228,10 @@
   <clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter
   match with the swap function signature, eliminating false-positives.
 
+- Improved :doc:`readability-braces-around-statements
+  <clang-tidy/checks/readability/braces-around-statements>` check to
+  ignore false-positive for ``if constexpr`` in lambda expression.
+
 - Improved :doc:`readability-container-size-empty
   <clang-tidy/checks/readability/container-size-empty>` check to
   detect comparison between string and empty string literals.
Index: clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
@@ -192,6 +192,9 @@
   while (const auto *AS = dyn_cast<AttributedStmt>(S))
     S = AS->getSubStmt();
 
+  const SourceManager &SM = *Result.SourceManager;
+  const ASTContext *Context = Result.Context;
+
   // 1) If there's a corresponding "else" or "while", the check inserts "} "
   // right before that token.
   // 2) If there's a multi-line block comment starting on the same line after
@@ -204,10 +207,15 @@
     return false;
   }
 
+  // When TreeTransform, Stmt in constexpr IfStmt will be transform to NullStmt.
+  // This NullStmt can be detected according to beginning token.
+  const SourceLocation StmtBeginLoc = S->getBeginLoc();
+  if (isa<NullStmt>(S) && StmtBeginLoc.isValid() &&
+      getTokenKind(StmtBeginLoc, SM, Context) == tok::l_brace)
+    return false;
+
   if (!InitialLoc.isValid())
     return false;
-  const SourceManager &SM = *Result.SourceManager;
-  const ASTContext *Context = Result.Context;
 
   // Convert InitialLoc to file location, if it's on the same macro expansion
   // level as the start of the statement. We also need file locations for


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158480.552227.patch
Type: text/x-patch
Size: 2970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230822/ba4aab31/attachment-0001.bin>


More information about the cfe-commits mailing list