[clang-tools-extra] [clang-tidy] Add `std::span` to default `bugprone-dangling-handle.HandleClasses` (PR #107711)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 20 12:19:42 PDT 2024


https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/107711

>From 8528404715b4ca6dec7e251548870aba0c64bb04 Mon Sep 17 00:00:00 2001
From: Afonso Faria <oracle-main at protonmail.com>
Date: Sat, 7 Sep 2024 18:03:48 +0100
Subject: [PATCH 1/3] Add std::span to default
 bugprone-dangling-handle.HandleClasses

---
 clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
index d55df3a6d7b741..e13b1ceacc5395 100644
--- a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
@@ -97,8 +97,8 @@ DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
                                          ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       HandleClasses(utils::options::parseStringList(Options.get(
-          "HandleClasses",
-          "std::basic_string_view;std::experimental::basic_string_view"))),
+          "HandleClasses", "std::basic_string_view;std::experimental::basic_"
+                           "string_view;std::span"))),
       IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {}
 
 void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {

>From ffbb34269627c4afe01bae908dc886e756e73254 Mon Sep 17 00:00:00 2001
From: Afonso Faria <oracle-main at protonmail.com>
Date: Sat, 7 Sep 2024 18:28:24 +0100
Subject: [PATCH 2/3] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8d028f8863cb7a..eec4f73459a229 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -108,6 +108,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
   the offending code with ``reinterpret_cast``, to more clearly express intent.
 
+- Improved :doc:`bugprone-dangling-handle
+  <clang-tidy/checks/bugprone/dangling-handle>` check to treat `std::span` as a
+  handle class.
+
 - Improved :doc:`modernize-use-std-format
   <clang-tidy/checks/modernize/use-std-format>` check to support replacing
   member function calls too.

>From 2f945d3f63718ed9ee257f77cb1726bd50eee72a Mon Sep 17 00:00:00 2001
From: Afonso Faria <oracle-main at protonmail.com>
Date: Thu, 12 Sep 2024 09:41:53 +0100
Subject: [PATCH 3/3] Update dangling-handle.rst

---
 .../clang-tidy/checks/bugprone/dangling-handle.rst    | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst
index 701b67d77acaa1..752b711b4ef545 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst
@@ -28,11 +28,18 @@ Examples:
     return Array;
   }
 
+  span<int> g() {
+    array<int, 1> V;
+    return {V};
+    int Array[10]{};
+    return {Array};
+  }
+
 Options
 -------
 
 .. option:: HandleClasses
 
    A semicolon-separated list of class names that should be treated as handles.
-   By default only ``std::basic_string_view`` and
-   ``std::experimental::basic_string_view`` are considered.
+   By default only ``std::basic_string_view``,
+   ``std::experimental::basic_string_view`` and ``std::span`` are considered.



More information about the cfe-commits mailing list