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

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 20 15:23:20 PDT 2024


Author: Afonso Faria
Date: 2024-09-21T00:23:17+02:00
New Revision: c24418ad914c64c78facb1bb77951b161448fcc0

URL: https://github.com/llvm/llvm-project/commit/c24418ad914c64c78facb1bb77951b161448fcc0
DIFF: https://github.com/llvm/llvm-project/commit/c24418ad914c64c78facb1bb77951b161448fcc0.diff

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

`std::span` suffers from the same dangling issues as `std::string_view`.
This patch adds `std::span` to the default list of handle classes in
`bugprone-dangling-handle`, allowing clang-tidy to catch e.g. the
following:
```cpp
span<int> f() {
  // All these return values will dangle.
  array<int, 1> A;
  return {A};

  vector<int> Array;
  return {Array};
}
```

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst

Removed: 
    


################################################################################
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) {

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d032cef6b76164..8f7b0b5333f3a1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -117,6 +117,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:`bugprone-forwarding-reference-overload
   <clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
   a crash when determining if an ``enable_if[_t]`` was found.

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