Skip to content

Commit 45411ac

Browse files
authored
[clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (llvm#134774)
**Edit:** I suggest we avoid diagnosing initializers for `std::array` type. The fixit provided is incorrect as observed in **llvm#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 llvm#133715
1 parent 561a21f commit 45411ac

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ void UseDesignatedInitializersCheck::registerMatchers(MatchFinder *Finder) {
121121
hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
122122
Finder->addMatcher(
123123
initListExpr(
124-
hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
125-
unless(HasBaseWithFields))
124+
hasType(cxxRecordDecl(
125+
RestrictToPODTypes ? isPOD() : isAggregate(),
126+
unless(anyOf(HasBaseWithFields, hasName("::std::array"))))
126127
.bind("type")),
127128
IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
128129
unless(isFullyDesignated()))

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ Changes in existing checks
182182
``constexpr`` and ``static``` values on member initialization and by detecting
183183
explicit casting of built-in types within member list initialization.
184184

185+
- Improved :doc:`modernize-use-designated-initializers
186+
<clang-tidy/checks/modernize/use-designated-initializers>` check by avoiding
187+
diagnosing designated initializers for ``std::array`` initializations.
188+
185189
- Improved :doc:`modernize-use-ranges
186190
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
187191
warnings logic for ``nullptr`` in ``std::find``.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-designated-initializers.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Options
5454

5555
The value `false` specifies that even initializers for aggregate types with
5656
only a single element should be checked. The default value is `true`.
57+
``std::array`` initializations are always excluded, as the type is a
58+
standard library abstraction and not intended to be initialized with
59+
designated initializers.
5760

5861
.. option:: RestrictToPODTypes
5962

clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,18 @@ struct S15{
209209
S15(S14& d):d{d}{}
210210
S14& d;
211211
};
212+
213+
//Issue #133715
214+
namespace std {
215+
template<typename T, unsigned int N>
216+
struct array {
217+
T __elems[N];
218+
};
219+
template<typename T, typename... U>
220+
array(T, U...) -> array<T, 1 + sizeof...(U)>;
221+
}
222+
223+
std::array a{1,2,3};
224+
std::array<int,2> b{10, 11};
225+
using array = std::array<int, 2>;
226+
array c{10, 11};

0 commit comments

Comments
 (0)