-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers #134774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-tidy Author: David Rivera (RiverDave) ChangesI suggest we avoid diagnosing designated initializers for Fun fact: std::array<int, 3> arr2 = {._M_elems[0] = {3}}; That compiles, however it wouldn’t make sense to provide this as suggestion considering it wouldn’t be portable at all. Full diff: https://github.com/llvm/llvm-project/pull/134774.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
index 3132067f3d5ec..9e2ac149d0868 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
@@ -119,13 +119,18 @@ UseDesignatedInitializersCheck::UseDesignatedInitializersCheck(
void UseDesignatedInitializersCheck::registerMatchers(MatchFinder *Finder) {
const auto HasBaseWithFields =
hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
+
+ // see #133715
+ const auto IsSTLArray =
+ hasType(qualType(hasDeclaration(recordDecl(hasName("::std::array")))));
+
Finder->addMatcher(
initListExpr(
hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
unless(HasBaseWithFields))
.bind("type")),
IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
- unless(isFullyDesignated()))
+ unless(anyOf(isFullyDesignated(), IsSTLArray)))
.bind("init"),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6c1f05009df98..44c348f453543 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -171,6 +171,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``.
|
@llvm/pr-subscribers-clang-tools-extra Author: David Rivera (RiverDave) ChangesI suggest we avoid diagnosing designated initializers for Fun fact: std::array<int, 3> arr2 = {._M_elems[0] = {3}}; That compiles, however it wouldn’t make sense to provide this as suggestion considering it wouldn’t be portable at all. Full diff: https://github.com/llvm/llvm-project/pull/134774.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
index 3132067f3d5ec..9e2ac149d0868 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
@@ -119,13 +119,18 @@ UseDesignatedInitializersCheck::UseDesignatedInitializersCheck(
void UseDesignatedInitializersCheck::registerMatchers(MatchFinder *Finder) {
const auto HasBaseWithFields =
hasAnyBase(hasType(cxxRecordDecl(has(fieldDecl()))));
+
+ // see #133715
+ const auto IsSTLArray =
+ hasType(qualType(hasDeclaration(recordDecl(hasName("::std::array")))));
+
Finder->addMatcher(
initListExpr(
hasType(cxxRecordDecl(RestrictToPODTypes ? isPOD() : isAggregate(),
unless(HasBaseWithFields))
.bind("type")),
IgnoreSingleElementAggregates ? hasMoreThanOneElement() : anything(),
- unless(isFullyDesignated()))
+ unless(anyOf(isFullyDesignated(), IsSTLArray)))
.bind("init"),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6c1f05009df98..44c348f453543 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -171,6 +171,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``.
|
Yes, this is required in order for |
clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
Outdated
Show resolved
Hide resolved
Actually, it seems to me that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix IgnoreSingleElementAggregates
instead.
Just to be clear, I suppose we're looking to fix |
clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
Outdated
Show resolved
Hide resolved
Ok, I see, let's disable for ::std::array then, generalize in the future if we come across a similar use case. |
9727ea1
to
65fc264
Compare
Thanks, your feedback has been addressed. |
65fc264
to
05b4a20
Compare
Ping |
clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
Outdated
Show resolved
Hide resolved
@@ -182,6 +182,11 @@ 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 when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document this exception also in IgnoreSingleElementAggregates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, let me know if my explanation is not clear enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a unit test demonstrating that the related issue is fixed.
…e-use-designated-initializers
05b4a20
to
5dc7aa5
Compare
Added 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exclude std::array don't resolve root problem here. It can also happen in user defined class.
e.g. https://godbolt.org/z/cn46MT5Mn
But maybe it could be handled in another PR? since std::array is a special case that we don't know the detail member name in std::array.
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