Skip to content

Commit 29e1df1

Browse files
jvoungjoaosaffran
authored and
joaosaffran
committed
[clang-tidy] Add a release note about unchecked-optional-access smart pointer caching (llvm#122290)
With caching added in llvm#120249, the `IgnoreSmartPointerDereference` option shouldn't be needed anymore. Other caching also added earlier: llvm#112605
1 parent 986725d commit 29e1df1

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ Changes in existing checks
108108
calls of ``std::string`` constructor with char pointer, start position and
109109
length parameters.
110110

111+
- Improved :doc:`bugprone-unchecked-optional-access
112+
<clang-tidy/checks/bugprone/unchecked-optional-access>` fixing false
113+
positives from smart pointer accessors repeated in checking ``has_value``
114+
and accessing ``value``. The option `IgnoreSmartPointerDereference` should
115+
no longer be needed and will be removed.
116+
111117
- Improved :doc:`bugprone-unsafe-functions
112118
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
113119
additional C++ member functions to match.

clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-optional-access.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ For example:
7171
.. code-block:: c++
7272

7373
void f(Foo foo) {
74-
if (foo.opt().has_value()) {
75-
use(*foo.opt()); // unsafe: it is unclear whether `foo.opt()` has a value.
74+
if (foo.take().has_value()) {
75+
use(*foo.take()); // unsafe: it is unclear whether `foo.take()` has a value.
7676
}
7777
}
7878
@@ -81,10 +81,11 @@ Exception: accessor methods
8181

8282
The check assumes *accessor* methods of a class are stable, with a heuristic to
8383
determine which methods are accessors. Specifically, parameter-free ``const``
84-
methods are treated as accessors. Note that this is not guaranteed to be safe
85-
-- but, it is widely used (safely) in practice, and so we have chosen to treat
86-
it as generally safe. Calls to non ``const`` methods are assumed to modify
87-
the state of the object and affect the stability of earlier accessor calls.
84+
methods and smart pointer-like APIs (non ``const`` overloads of ``*`` when
85+
there is a parallel ``const`` overload) are treated as accessors. Note that
86+
this is not guaranteed to be safe -- but, it is widely used (safely) in
87+
practice. Calls to non ``const`` methods are assumed to modify the state of
88+
the object and affect the stability of earlier accessor calls.
8889

8990
Rely on invariants of uncommon APIs
9091
-----------------------------------
@@ -191,14 +192,15 @@ paths that lead to an access. For example:
191192
Stabilize function results
192193
~~~~~~~~~~~~~~~~~~~~~~~~~~
193194

194-
Since function results are not assumed to be stable across calls, it is best to
195-
store the result of the function call in a local variable and use that variable
196-
to access the value. For example:
195+
Function results are not assumed to be stable across calls, except for
196+
const accessor methods. For more complex accessors (non-const, or depend on
197+
multiple params) it is best to store the result of the function call in a
198+
local variable and use that variable to access the value. For example:
197199

198200
.. code-block:: c++
199201

200202
void f(Foo foo) {
201-
if (const auto& foo_opt = foo.opt(); foo_opt.has_value()) {
203+
if (const auto& foo_opt = foo.take(); foo_opt.has_value()) {
202204
use(*foo_opt);
203205
}
204206
}

0 commit comments

Comments
 (0)