[clang] [clang] Enable the -Wdangling-capture diagnostic by default. (PR #119685)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 12 01:58:48 PST 2024


https://github.com/hokein created https://github.com/llvm/llvm-project/pull/119685

We have tested this diagnostics internally, and we don't find see any issues.

>From be7678e1798ad61af05ac3f83d7149125be9ed7a Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Thu, 12 Dec 2024 10:57:06 +0100
Subject: [PATCH] [clang] Enable the -Wdangling-capture diagnostic by default.

---
 clang/docs/ReleaseNotes.rst                      | 9 +++++++++
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6802c0c50b8f0c..7cb18225cb38c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -664,6 +664,15 @@ Improvements to Clang's diagnostics
       bool operator==(const C&) = default;
     };
 
+- Clang now emits `-Wdangling-capture` diangostic when a STL container captures a dangling reference.
+
+  .. code-block:: c++
+
+    void test() {
+      std::vector<std::string_view> views;
+      views.push_back(std::string("123")); // warning
+    }
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..811265151fa0da 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10237,10 +10237,10 @@ def warn_dangling_pointer_assignment : Warning<
    InGroup<DanglingAssignment>;
 def warn_dangling_reference_captured : Warning<
    "object whose reference is captured by '%0' will be destroyed at the end of "
-   "the full-expression">, InGroup<DanglingCapture>, DefaultIgnore;
+   "the full-expression">, InGroup<DanglingCapture>;
 def warn_dangling_reference_captured_by_unknown : Warning<
    "object whose reference is captured will be destroyed at the end of "
-   "the full-expression">, InGroup<DanglingCapture>, DefaultIgnore;
+   "the full-expression">, InGroup<DanglingCapture>;
 
 // For non-floating point, expressions of the form x == x or x != x
 // should result in a warning, since these always evaluate to a constant.



More information about the cfe-commits mailing list