Skip to content

Fix false positive of [[clang::require_explicit_initialization]] on copy/move constructors #126553

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4576,7 +4576,9 @@ static void TryConstructorInitialization(Sema &S,
if (!IsListInit &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this if condition is becoming complex enough that it's worth adding a comment for explanation.

(Kind.getKind() == InitializationKind::IK_Default ||
Kind.getKind() == InitializationKind::IK_Direct) &&
DestRecordDecl != nullptr && DestRecordDecl->isAggregate() &&
DestRecordDecl != nullptr &&
!(CtorDecl->isCopyOrMoveConstructor() && CtorDecl->isImplicit()) &&
DestRecordDecl->isAggregate() &&
DestRecordDecl->hasUninitializedExplicitInitFields()) {
S.Diag(Kind.getLocation(), diag::warn_field_requires_explicit_init)
<< /* Var-in-Record */ 1 << DestRecordDecl;
Expand Down
34 changes: 33 additions & 1 deletion clang/test/SemaCXX/uninitialized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,9 +1542,15 @@ void aggregate() {
};
};

struct CopyAndMove {
CopyAndMove() = default;
CopyAndMove(const CopyAndMove &) {}
CopyAndMove(CopyAndMove &&) {}
};
struct Embed {
int embed1; // #FIELD_EMBED1
int embed2 [[clang::require_explicit_initialization]]; // #FIELD_EMBED2
CopyAndMove force_separate_move_ctor;
};
struct EmbedDerived : Embed {};
struct F {
Expand Down Expand Up @@ -1582,7 +1588,33 @@ void aggregate() {
F("___"),
F("____")
};
(void)ctors;

struct MoveOrCopy {
Embed e;
EmbedDerived ed;
F f;
// no-error
MoveOrCopy(const MoveOrCopy &c) : e(c.e), ed(c.ed), f(c.f) {}
// no-error
MoveOrCopy(MoveOrCopy &&c)
: e(std::move(c.e)), ed(std::move(c.ed)), f(std::move(c.f)) {}
};
F copy1(ctors[0]); // no-error
(void)copy1;
F move1(std::move(ctors[0])); // no-error
(void)move1;
F copy2{ctors[0]}; // no-error
(void)copy2;
F move2{std::move(ctors[0])}; // no-error
(void)move2;
F copy3 = ctors[0]; // no-error
(void)copy3;
F move3 = std::move(ctors[0]); // no-error
(void)move3;
F copy4 = {ctors[0]}; // no-error
(void)copy4;
F move4 = {std::move(ctors[0])}; // no-error
(void)move4;

S::foo(S{1, 2, 3, 4});
S::foo(S{.s1 = 100, .s4 = 100});
Expand Down