diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp @@ -192,6 +192,9 @@ while (const auto *AS = dyn_cast(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(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 diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -228,6 +228,10 @@ ` check to enforce a stricter match with the swap function signature, eliminating false-positives. +- Improved :doc:`readability-braces-around-statements + ` check to + ignore false-positive for ``if constexpr`` in lambda expression. + - Improved :doc:`readability-container-size-empty ` check to detect comparison between string and empty string literals. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/braces-around-statements.cpp @@ -457,3 +457,25 @@ // CHECK-FIXES-NEXT: } } + +template +auto constexpr_lambda_1 = [] { + if constexpr (A) { + 1; + } +}; + +template +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(); + constexpr_lambda_2(); +}