Skip to content

Commit 041b369

Browse files
committed
[clang] Detect dangling assignment for "Container<Pointer>" case.
1 parent e50131a commit 041b369

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ Improvements to Clang's diagnostics
300300

301301
- Clang now diagnoses cases where a dangling ``GSLOwner<GSLPointer>`` object is constructed, e.g. ``std::vector<string_view> v = {std::string()};`` (#GH100526).
302302

303+
- Clang now diagnoses cases where a dangling ``GSLOwner<GSLPointer>`` object is assigned, e.g. ``v = {std::string()};`` (#GH100526).
304+
303305
Improvements to Clang's time-trace
304306
----------------------------------
305307

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ static bool shouldRunGSLAssignmentAnalysis(const Sema &SemaRef,
982982
diag::warn_dangling_lifetime_pointer_assignment, SourceLocation());
983983
return (EnableGSLAssignmentWarnings &&
984984
(isRecordWithAttr<PointerAttr>(Entity.LHS->getType()) ||
985-
isAssignmentOperatorLifetimeBound(Entity.AssignmentOperator)));
985+
isAssignmentOperatorLifetimeBound(Entity.AssignmentOperator) ||
986+
isContainerOfPointer(Entity.LHS->getType()->getAsRecordDecl())));
986987
}
987988

988989
static void checkExprLifetimeImpl(Sema &SemaRef,

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,17 +601,23 @@ void test() {
601601
std::optional<std::string_view> o4 = std::optional<std::string_view>(s);
602602

603603
// FIXME: should work for assignment cases
604-
v1 = {std::string()};
605-
o1 = std::string();
604+
v1 = {std::string()}; // expected-warning {{object backing the pointer}}
605+
o1 = std::string(); // expected-warning {{object backing the pointer}}
606606

607607
// no warning on copying pointers.
608608
std::vector<std::string_view> n1 = {std::string_view()};
609+
n1 = {std::string_view()};
609610
std::optional<std::string_view> n2 = {std::string_view()};
611+
n2 = {std::string_view()};
610612
std::optional<std::string_view> n3 = std::string_view();
613+
n3 = std::string_view();
611614
std::optional<std::string_view> n4 = std::make_optional(std::string_view());
615+
n4 = std::make_optional(std::string_view());
612616
const char* b = "";
613617
std::optional<std::string_view> n5 = std::make_optional(b);
618+
n5 = std::make_optional(b);
614619
std::optional<std::string_view> n6 = std::make_optional("test");
620+
n6 = std::make_optional("test");
615621
}
616622

617623
std::vector<std::string_view> test2(int i) {

0 commit comments

Comments
 (0)