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
[clang-tidy] cppcoreguidelines-pro-type-member-init check flags anonymous union member #54748
Comments
@llvm/issue-subscribers-clang-tidy |
The cppcoreguidelines-pro-type-member-init.cpp file has a test that ensures the check does not flag a union with only one member initialized: llvm-project/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp Line 296 in 6f73bd7
and a test that ensures the check does flag members of anonymous unions when none are initialized: llvm-project/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp Line 332 in 6f73bd7
(both authored by @alexfh). |
A similar issue was reported in 2017: #34232 At the time, @JonasToth suggested the bug might be related to this commit: llvm-mirror/clang-tools-extra@b7c157e Since then, a commit by @Sockke was added to fix an apparently related issue: e490248 |
The same warning is emitted also for when initializing one of the union members in a mem-init-list (as opposed to default mem initializers in the union), flagging e.g. the idiomatic std::optional backports for constexpr default-construction of disengaged optionals: struct Optional {
// warning: constructor does not initialize these fields: value_
// [cppcoreguidelines-pro-type-member-init,hicpp-member-init]
constexpr Optional() : dummy_{} {}
union {
char dummy_;
int value_;
};
}; |
Btw, the review differential of the fix patched by @Sockke is https://reviews.llvm.org/D108370. IMHO, clang-tidy should be conservative when emitting the diagnostics, if the clang-tidy cannot afford the overhead or has no confidence to cover all the cases, it should give up emitting the warnings. |
The latest patch is here: https://reviews.llvm.org/D127293, and there are still omissions in the processing of a union. |
…nitialized in cppcoreguidelines-pro-type-member-init If a union member is initialized, the other members of the union are still suggested to be initialized in this check. This patch fixes this behavior. Reference issue: #54748 Reviewed By: njames93 Differential Revision: https://reviews.llvm.org/D127293
Fixed |
Thanks for your work fixing this and getting it through review @Sockke! |
…nitialized in cppcoreguidelines-pro-type-member-init If a union member is initialized, other members are still recorded in the container to be initialized. This patch fixes this behavior. Reference issue: #54748 Differential Revision: https://reviews.llvm.org/D127293
Running clang-tidy on this code:
results in the following warning:
However, adding an initializer to
b
(e.g.int b = 0
) results in a clang diagnostic:The text was updated successfully, but these errors were encountered: