[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 20:24:12 PDT 2023
HerrCai0907 created this revision.
Herald added subscribers: PiotrZSL, carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
HerrCai0907 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Fixed: #64545
When TreeTransform, Stmt in constexpr IfStmt will be transform to NullStmt.
This NullStmt has the different beginning token.
This patch add addtional check in checkStmt to handle this case.
Repository:
rG LLVM Github Monorepo
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 constexpr if statement 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.552194.patch
Type: text/x-patch
Size: 2976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230822/c770e714/attachment.bin>
More information about the cfe-commits
mailing list