[clang-tools-extra] [clang-tidy] comment braced-init list arguments (PR #180408)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 8 08:38:19 PST 2026
https://github.com/unterumarmung updated https://github.com/llvm/llvm-project/pull/180408
>From 0583fed4e6e4082aa7be5101b0171c7fdae144ab Mon Sep 17 00:00:00 2001
From: Daniil Dudkin <unterumarmung at yandex.ru>
Date: Sun, 8 Feb 2026 16:23:22 +0300
Subject: [PATCH 1/2] clang-tidy: comment braced-init list arguments
Handle braced-init list arguments in bugprone-argument-comment and\nadd coverage for initializer_list and designated initializers.
---
.../bugprone/ArgumentCommentCheck.cpp | 30 ++++++-
clang-tools-extra/docs/ReleaseNotes.rst | 5 +-
.../checks/bugprone/argument-comment.rst | 2 +
.../bugprone/argument-comment-literals.cpp | 80 ++++++++++++++++++-
4 files changed, 111 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
index d46896808bd09..86c1252b5e913 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
@@ -8,6 +8,7 @@
#include "ArgumentCommentCheck.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/ExprCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Token.h"
@@ -242,14 +243,37 @@ static const FunctionDecl *resolveMocks(const FunctionDecl *Func) {
return Func;
}
+static bool isBracedInitListExpr(const Expr *Arg) {
+ Arg = Arg->IgnoreImplicit();
+ // List-initialization can be represented by a list expression itself or by a
+ // list-initialized constructor/cast. Treat all of these as braced-init uses.
+ if (isa<InitListExpr>(Arg))
+ return true;
+ if (const auto *StdInit = dyn_cast<CXXStdInitializerListExpr>(Arg))
+ return isBracedInitListExpr(StdInit->getSubExpr()->IgnoreImplicit());
+ if (const auto *Ctor = dyn_cast<CXXConstructExpr>(Arg))
+ return Ctor->isListInitialization();
+ if (const auto *FuncCast = dyn_cast<CXXFunctionalCastExpr>(Arg))
+ return FuncCast->isListInitialization();
+ return false;
+}
+
// Given the argument type and the options determine if we should
// be adding an argument comment.
bool ArgumentCommentCheck::shouldAddComment(const Expr *Arg) const {
- Arg = Arg->IgnoreImpCasts();
- if (isa<UnaryOperator>(Arg))
- Arg = cast<UnaryOperator>(Arg)->getSubExpr();
+ // Strip implicit wrappers so brace-init arguments bound to references still
+ // look like list-initialization at this point.
+ Arg = Arg->IgnoreImplicit();
+ if (const auto *UO = dyn_cast<UnaryOperator>(Arg))
+ Arg = UO->getSubExpr()->IgnoreImplicit();
if (Arg->getExprLoc().isMacroID())
return false;
+
+ // Braced-init list arguments (e.g. {} or Type{}) are commonly unclear even
+ // when the type name is explicit, so always suggest a parameter comment.
+ if (isBracedInitListExpr(Arg))
+ return true;
+
return (CommentBoolLiterals && isa<CXXBoolLiteralExpr>(Arg)) ||
(CommentIntegerLiterals && isa<IntegerLiteral>(Arg)) ||
(CommentFloatLiterals && isa<FloatingLiteral>(Arg)) ||
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7b5b332bdb8a2..cf5667cde6205 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -141,8 +141,9 @@ Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^
- Improved :doc:`bugprone-argument-comment
- <clang-tidy/checks/bugprone/argument-comment>` to also check for C++11
- inherited constructors.
+ <clang-tidy/checks/bugprone/argument-comment>`:
+ - Also checks for C++11 inherited constructors.
+ - Comments braced-init list arguments such as ``{}`` and ``Type{}``.
- Improved :doc:`bugprone-macro-parentheses
<clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.rst
index 8770d7224137a..2c495df82752e 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/argument-comment.rst
@@ -18,6 +18,8 @@ that are placed right before the argument.
// warning: argument name 'bar' in comment does not match parameter name 'foo'
The check tries to detect typos and suggest automated fixes for them.
+It also suggests comments for braced-init list arguments (e.g., ``{}`` or
+``Type{}``), since the intent is often unclear at the call site.
Options
-------
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-literals.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-literals.cpp
index f03488a14d9f5..9fac137a8d74c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-literals.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/argument-comment-literals.cpp
@@ -1,4 +1,13 @@
-// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- \
+// RUN: %check_clang_tidy -std=c++11 %s bugprone-argument-comment %t -- \
+// RUN: -config="{CheckOptions: { \
+// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \
+// RUN: bugprone-argument-comment.CommentIntegerLiterals: true, \
+// RUN: bugprone-argument-comment.CommentFloatLiterals: true, \
+// RUN: bugprone-argument-comment.CommentUserDefinedLiterals: true, \
+// RUN: bugprone-argument-comment.CommentStringLiterals: true, \
+// RUN: bugprone-argument-comment.CommentNullPtrs: true, \
+// RUN: bugprone-argument-comment.CommentCharacterLiterals: true}}" --
+// RUN: %check_clang_tidy -check-suffixes=,CXX20 -std=c++20 %s bugprone-argument-comment %t -- \
// RUN: -config="{CheckOptions: { \
// RUN: bugprone-argument-comment.CommentBoolLiterals: true, \
// RUN: bugprone-argument-comment.CommentIntegerLiterals: true, \
@@ -185,3 +194,72 @@ void ignores_underscores() {
// CHECK-MESSAGES: [[@LINE-1]]:5: warning: argument comment missing for literal argument
// CHECK-FIXES: f(/*_with_underscores_=*/true);
}
+
+namespace std {
+using size_t = decltype(sizeof(0));
+
+template <typename T>
+class vector {
+public:
+ vector();
+};
+
+template <typename T>
+class initializer_list {
+ const T *Begin;
+ const T *End;
+
+public:
+ initializer_list() : Begin(nullptr), End(nullptr) {}
+ const T *begin() const { return Begin; }
+ const T *end() const { return End; }
+ size_t size() const { return static_cast<size_t>(End - Begin); }
+};
+} // namespace std
+
+namespace GH171842 {
+
+struct T {
+ int value;
+};
+
+struct Agg {
+ int x;
+ int y;
+};
+
+void foo(T some_arg, const std::vector<int> &dims);
+void foo_init_list(T some_arg, std::initializer_list<int> dims);
+void foo_designated(T some_arg, const Agg &dims);
+
+void test_braced_init_list() {
+ T some_arg{0};
+
+ foo(some_arg, {});
+ // CHECK-MESSAGES: [[@LINE-1]]:17: warning: argument comment missing for literal argument 'dims' [bugprone-argument-comment]
+ // CHECK-FIXES: foo(some_arg, /*dims=*/{});
+
+ foo(some_arg, std::vector<int>{});
+ // CHECK-MESSAGES: [[@LINE-1]]:17: warning: argument comment missing for literal argument 'dims' [bugprone-argument-comment]
+ // CHECK-FIXES: foo(some_arg, /*dims=*/std::vector<int>{});
+}
+
+void test_initializer_list() {
+ T some_arg{0};
+
+ foo_init_list(some_arg, {1, 2, 3});
+ // CHECK-MESSAGES: [[@LINE-1]]:27: warning: argument comment missing for literal argument 'dims' [bugprone-argument-comment]
+ // CHECK-FIXES: foo_init_list(some_arg, /*dims=*/{1, 2, 3});
+}
+
+// C++20-only syntax is exercised in the separate C++20 run above.
+#if __cplusplus >= 202002L
+void test_designated_init() {
+ T some_arg{0};
+
+ foo_designated(some_arg, Agg{.x = 1});
+ // CHECK-MESSAGES-CXX20: [[@LINE-1]]:28: warning: argument comment missing for literal argument 'dims' [bugprone-argument-comment]
+ // CHECK-FIXES-CXX20: foo_designated(some_arg, /*dims=*/Agg{.x = 1});
+}
+#endif
+} // namespace GH171842
>From 5a63651a9fc81a084b798cd60c9ba6bebe423b84 Mon Sep 17 00:00:00 2001
From: Daniil Dudkin <unterumarmung at yandex.ru>
Date: Sun, 8 Feb 2026 19:38:11 +0300
Subject: [PATCH 2/2] Update clang-tools-extra/docs/ReleaseNotes.rst
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index cf5667cde6205..7f0aa44bb9c45 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -142,7 +142,9 @@ Changes in existing checks
- Improved :doc:`bugprone-argument-comment
<clang-tidy/checks/bugprone/argument-comment>`:
+
- Also checks for C++11 inherited constructors.
+
- Comments braced-init list arguments such as ``{}`` and ``Type{}``.
- Improved :doc:`bugprone-macro-parentheses
More information about the cfe-commits
mailing list