[clang] 90192e8 - Fix false positive of [[clang::require_explicit_initialization]] on copy/move constructors (#126553)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 10 22:52:16 PST 2025
Author: higher-performance
Date: 2025-02-11T07:52:13+01:00
New Revision: 90192e8872cc90b4d292b180a49babf72d17e579
URL: https://github.com/llvm/llvm-project/commit/90192e8872cc90b4d292b180a49babf72d17e579
DIFF: https://github.com/llvm/llvm-project/commit/90192e8872cc90b4d292b180a49babf72d17e579.diff
LOG: Fix false positive of [[clang::require_explicit_initialization]] on copy/move constructors (#126553)
Fixes #126490
Added:
Modified:
clang/lib/Sema/SemaInit.cpp
clang/test/SemaCXX/uninitialized.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 308222a79d920c..18090eb1c9e9ac 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -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;
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp
index 7578b288d7b3fe..4af2c998f082e5 100644
--- a/clang/test/SemaCXX/uninitialized.cpp
+++ b/clang/test/SemaCXX/uninitialized.cpp
@@ -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 {
@@ -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});
More information about the cfe-commits
mailing list