Skip to content

Commit

Permalink
[clang-tidy] Add std::span to default `bugprone-dangling-handle.Han…
Browse files Browse the repository at this point in the history
…dleClasses` (llvm#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};
}
```
  • Loading branch information
oracle-9 authored Sep 20, 2024
1 parent 4b4ea6d commit c24418a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit c24418a

Please sign in to comment.