[clang] Fix `-Wlifetime-safety-dangling-global-moved` not being implied by `-Wlifetime-safety-all` (PR #204891)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 20 12:15:32 PDT 2026
https://github.com/sipher-01 updated https://github.com/llvm/llvm-project/pull/204891
>From 3307e7c247e3b0cc64b8cdc9b59349bd2133c680 Mon Sep 17 00:00:00 2001
From: Sipher <sourav2003singhkatoch at gmail.com>
Date: Sat, 20 Jun 2026 02:06:18 +0530
Subject: [PATCH 1/2] [Clang][LifetimeSafety] Add DanglingGlobalMoved to
lifetime-safety-strict group
lifetime-safety-dangling-global-moved was defined but not included in
any parent diagnostic group, meaning -Wlifetime-safety-all and
-Wno-lifetime-safety-all had no effect on it. Add it to
LifetimeSafetyStrict alongside its sibling -moved warnings.
Fixes #204213
---
clang/include/clang/Basic/DiagnosticGroups.td | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 244cd3630bb11..5cdebd35ba05d 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -663,6 +663,7 @@ def LifetimeSafetyStrict : DiagGroup<"lifetime-safety-strict",
LifetimeSafetyUseAfterScopeMoved,
LifetimeSafetyReturnStackAddrMoved,
LifetimeSafetyDanglingFieldMoved,
+ LifetimeSafetyDanglingGlobalMoved,
LifetimeSafetyInvalidation]>;
def LifetimeSafety : DiagGroup<"lifetime-safety",
>From d18f74def1e15518f0f272e33420161417c54e78 Mon Sep 17 00:00:00 2001
From: Sipher <sourav2003singhkatoch at gmail.com>
Date: Sun, 21 Jun 2026 00:44:42 +0530
Subject: [PATCH 2/2] [Clang][LifetimeSafety] Add test for
dangling-global-moved warning
---
clang/test/Sema/LifetimeSafety/dangling-global.cpp | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/clang/test/Sema/LifetimeSafety/dangling-global.cpp b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
index 8a96cbced43b4..8609bd6136ec1 100644
--- a/clang/test/Sema/LifetimeSafety/dangling-global.cpp
+++ b/clang/test/Sema/LifetimeSafety/dangling-global.cpp
@@ -1,8 +1,14 @@
-// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-dangling -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-lifetime-safety-dangling-global-moved -Wno-dangling -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-dangling-global-moved -Wno-dangling -verify=moved %s
+
+#include "Inputs/lifetime-analysis.h"
int *global; // expected-note 4 {{this global dangles}}
int *global_backup; // expected-note {{this global dangles}}
+std::string_view global_view; // moved-note {{this global dangles}}
+void takeString(std::string&& s);
+
struct ObjWithStaticField {
static int *static_field; // expected-note {{this static storage dangles}}
};
@@ -70,3 +76,9 @@ void conditional_no_escape(int c) {
global = nullptr; // no-warning
(void)local;
}
+
+//when a local string is stored in a global view and then moved, the analyzer warns it "may" dangle since the storage may have been moved
+void store_local_in_global_but_moved(std::string s){
+ global_view = s; // moved-warning-re {{stack memory associated with parameter 's' may escape to the global variable 'global_view' which will dangle{{.*}} may have been moved}}
+ takeString(std::move(s)); //moved-note {{potentially moved here}}
+}
\ No newline at end of file
More information about the cfe-commits
mailing list