https://github.com/voyager-jhk updated 
https://github.com/llvm/llvm-project/pull/220548

>From 3975113ab692e48ad8568f2c72cdc76fbc178406 Mon Sep 17 00:00:00 2001
From: voyager-jhk <[email protected]>
Date: Wed, 2 Sep 2026 18:44:26 +0800
Subject: [PATCH] [clang-tidy] Fix false positive in readability-trailing-comma

Use the syntactic form of an empty InitListExpr instead of falling back to the 
semantic form.
---
 .../clang-tidy/readability/TrailingCommaCheck.cpp      |  6 ++++--
 clang-tools-extra/docs/ReleaseNotes.md                 |  5 +++++
 .../checkers/readability/trailing-comma-cxx11.cpp      | 10 ++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
index cb1a33ba09233..d687980ed0999 100644
--- a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
@@ -122,9 +122,11 @@ void TrailingCommaCheck::checkEnumDecl(const EnumDecl 
*Enum,
 void TrailingCommaCheck::checkInitListExpr(
     const InitListExpr *InitList, const MatchFinder::MatchResult &Result) {
   // We need to use non-empty syntactic form for correct source locations.
-  if (const InitListExpr *SynInitInitList = InitList->getSyntacticForm();
-      SynInitInitList && SynInitInitList->getNumInits() > 0)
+  if (const InitListExpr *SynInitInitList = InitList->getSyntacticForm()) {
+    if (SynInitInitList->getNumInits() == 0)
+      return;
     InitList = SynInitInitList;
+  }
 
   const bool IsSingleLine = isSingleLine(
       {InitList->getBeginLoc(), InitList->getEndLoc()}, *Result.SourceManager);
diff --git a/clang-tools-extra/docs/ReleaseNotes.md 
b/clang-tools-extra/docs/ReleaseNotes.md
index 633418a2abb98..64973fb0afb9e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -223,6 +223,11 @@ infrastructure are described first, followed by 
tool-specific sections.
   for intermediate subobjects caused the trailing comma of the enclosing
   list to be incorrectly rewritten.
 
+- Improved {doc}`readability-trailing-comma
+  <clang-tidy/checks/readability/trailing-comma>` check by fixing a false
+  positive on empty brace initializers of types with default member
+  initializers.
+
 - Improved {doc}`readability-use-std-min-max
   <clang-tidy/checks/readability/use-std-min-max>` check by fixing spurious
   trailing semicolons and lost comments when the `if` body has no braces.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-cxx11.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-cxx11.cpp
index 9f37db2c837c3..5d836b7727082 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-cxx11.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-cxx11.cpp
@@ -54,3 +54,13 @@ struct PackSingle {
 
 PackSingle<int> p1;
 PackSingle<int, double, char> p3;
+
+struct WithDefault { int foo = 1; };
+void takesTwo(WithDefault, int);
+
+void emptyInitListWithDefaultMember() {
+  takesTwo(WithDefault{}, 1);
+  int a[] = {1,};
+  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: initializer list should not 
have a trailing comma
+  // CHECK-FIXES: int a[] = {1};
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to