[clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (#134774)

**Edit:**
I suggest we avoid diagnosing initializers for `std::array` type. The
fixit provided is incorrect as observed in **#133715.** The only
workaround would require C99-style array designators which don’t really
align with the purpose of this check. This would also generate extra
compiler warnings.

Fixes #133715
This commit is contained in:
David Rivera
2025-04-28 14:12:38 -04:00
committed by GitHub
parent 561a21f18c
commit 45411ac895
4 changed files with 25 additions and 2 deletions

View File

@@ -121,8 +121,9 @@ void UseDesignatedInitializersCheck::registerMatchers(MatchFinder *Finder) {
hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
Finder->addMatcher(
initListExpr(
hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
unless(HasBaseWithFields))
hasType(cxxRecordDecl(
RestrictToPODTypes ? isPOD() : isAggregate(),
unless(anyOf(HasBaseWithFields, hasName("::std::array"))))
.bind("type")),
IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
unless(isFullyDesignated()))

View File

@@ -182,6 +182,10 @@ Changes in existing checks
``constexpr`` and ``static``` values on member initialization and by detecting
explicit casting of built-in types within member list initialization.
- Improved :doc:`modernize-use-designated-initializers
<clang-tidy/checks/modernize/use-designated-initializers>` check by avoiding
diagnosing designated initializers for ``std::array`` initializations.
- Improved :doc:`modernize-use-ranges
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
warnings logic for ``nullptr`` in ``std::find``.

View File

@@ -54,6 +54,9 @@ Options
The value `false` specifies that even initializers for aggregate types with
only a single element should be checked. The default value is `true`.
``std::array`` initializations are always excluded, as the type is a
standard library abstraction and not intended to be initialized with
designated initializers.
.. option:: RestrictToPODTypes

View File

@@ -209,3 +209,18 @@ struct S15{
S15(S14& d):d{d}{}
S14& d;
};
//Issue #133715
namespace std {
template<typename T, unsigned int N>
struct array {
T __elems[N];
};
template<typename T, typename... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;
}
std::array a{1,2,3};
std::array<int,2> b{10, 11};
using array = std::array<int, 2>;
array c{10, 11};