@@ -12,15 +12,6 @@ after the call. When the function returns such a parameter also as constant refe
1212then the returned reference can be used after the object it refers to has been
1313destroyed.
1414
15- This issue can be resolved by declaring an overload of the problematic function
16- where the ``const & `` parameter is instead declared as ``&& ``. The developer has
17- to ensure that the implementation of that function does not produce a
18- use-after-free, the exact error that this check is warning against.
19- Marking such an ``&& `` overload as ``deleted ``, will silence the warning as
20- well. In the case of different ``const & `` parameters being returned depending
21- on the control flow of the function, an overload where all problematic
22- ``const & `` parameters have been declared as ``&& `` will resolve the issue.
23-
2415Example
2516-------
2617
@@ -38,3 +29,23 @@ Example
3829
3930 const S& s = fn(S{1});
4031 s.v; // use after free
32+
33+
34+ This issue can be resolved by declaring an overload of the problematic function
35+ where the ``const & `` parameter is instead declared as ``&& ``. The developer has
36+ to ensure that the implementation of that function does not produce a
37+ use-after-free, the exact error that this check is warning against.
38+ Marking such an ``&& `` overload as ``deleted ``, will silence the warning as
39+ well. In the case of different ``const & `` parameters being returned depending
40+ on the control flow of the function, an overload where all problematic
41+ ``const & `` parameters have been declared as ``&& `` will resolve the issue.
42+
43+ This issue can also be resolved by adding ``[[clang::lifetimebound]] ``. Clang
44+ enable ``-Wdangling `` warning by default which can detect mis-uses of the
45+ annotated function. See `lifetimebound attribute <https://clang.llvm.org/docs/AttributeReference.html#id11 >`_
46+ for details.
47+
48+ .. code-block :: c++
49+
50+ const int &f(const int &a [[clang::lifetimebound]]) { return a; } // no warning
51+ const int &v = f(1); // warning: temporary bound to local reference 'v' will be destroyed at the end of the full-expression [-Wdangling]
0 commit comments