Skip to content

Commit

Permalink
Fix false positive of [[clang::require_explicit_initialization]] on c…
Browse files Browse the repository at this point in the history
…opy/move constructors (#126553)

Fixes #126490
  • Loading branch information
higher-performance authored Feb 11, 2025
1 parent be354cf commit 90192e8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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 &&
(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

0 comments on commit 90192e8

Please sign in to comment.