Author: Congcong Cai Date: 2023-08-22T14:21:59+08:00 New Revision: 9b6859dc89b9578cc31811e6c122ed79ec58dac3
URL: https://github.com/llvm/llvm-project/commit/9b6859dc89b9578cc31811e6c122ed79ec58dac3 DIFF: https://github.com/llvm/llvm-project/commit/9b6859dc89b9578cc31811e6c122ed79ec58dac3.diff LOG: [clang-tidy][readability-braces-around-statements] ignore false-positive for constexpr if statement in lambda expression 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. Reviewed By: PiotrZSL Differential Revision: https://reviews.llvm.org/D158480 Added: Modified: 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 Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp index 74bb073848a6cd..1dfa9dce0c52b1 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<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 @@ 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<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 diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 28a386e529c84f..7b00a0a6ddc7f6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -228,6 +228,10 @@ Changes in existing checks <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. 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 494c2b780a7cda..ff8488d2c6de3d 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 <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>(); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits