diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp index 74bb073848a6c..1dfa9dce0c52b 100644 --- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp @@ -192,6 +192,9 @@ bool BracesAroundStatementsCheck::checkStmt( 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 @@ bool BracesAroundStatementsCheck::checkStmt( 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 index 28a386e529c84..7b00a0a6ddc7f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -228,6 +228,10 @@ Changes in existing checks ` 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 index 494c2b780a7cd..ff8488d2c6de3 100644 --- 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 @@ int test_macros(bool b) { // 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(); +}