Skip to content

Commit c24418a

Browse files
authored
[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}; } ```
1 parent 4b4ea6d commit c24418a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
9797
ClangTidyContext *Context)
9898
: ClangTidyCheck(Name, Context),
9999
HandleClasses(utils::options::parseStringList(Options.get(
100-
"HandleClasses",
101-
"std::basic_string_view;std::experimental::basic_string_view"))),
100+
"HandleClasses", "std::basic_string_view;std::experimental::basic_"
101+
"string_view;std::span"))),
102102
IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {}
103103

104104
void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ Changes in existing checks
117117
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
118118
the offending code with ``reinterpret_cast``, to more clearly express intent.
119119

120+
- Improved :doc:`bugprone-dangling-handle
121+
<clang-tidy/checks/bugprone/dangling-handle>` check to treat `std::span` as a
122+
handle class.
123+
120124
- Improved :doc:`bugprone-forwarding-reference-overload
121125
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
122126
a crash when determining if an ``enable_if[_t]`` was found.

clang-tools-extra/docs/clang-tidy/checks/bugprone/dangling-handle.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ Examples:
2828
return Array;
2929
}
3030

31+
span<int> g() {
32+
array<int, 1> V;
33+
return {V};
34+
int Array[10]{};
35+
return {Array};
36+
}
37+
3138
Options
3239
-------
3340

3441
.. option:: HandleClasses
3542

3643
A semicolon-separated list of class names that should be treated as handles.
37-
By default only ``std::basic_string_view`` and
38-
``std::experimental::basic_string_view`` are considered.
44+
By default only ``std::basic_string_view``,
45+
``std::experimental::basic_string_view`` and ``std::span`` are considered.

0 commit comments

Comments
 (0)