Skip to content

Commit c5e6938

Browse files
authored
[clang-tidy][docs][NFC] Add limitation of variable lifetimes in performance-unnecessary-copy-initialization (#151862)
Addresses #150189.
1 parent 0cf7377 commit c5e6938

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,22 @@ Options
5959
types that don't own the underlying data. Like for `AllowedTypes` above,
6060
regular expressions are accepted and the inclusion of `::` determines whether
6161
the qualified typename is matched or not.
62+
63+
64+
Limitations
65+
-----------
66+
67+
This check does not perform lifetime analysis and may suggest replacing copies
68+
with const references that could become dangling. Be cautious when the
69+
referenced object might be invalidated by subsequent operations.
70+
71+
.. code-block:: c++
72+
73+
void consume(const S&);
74+
75+
void func(std::vector<S> &Vec) {
76+
const auto It = Vec.begin();
77+
const S Value(*It); // The warning will suggest making this a const reference.
78+
Vec.erase(It); // Container modifications could invalidate references.
79+
consume(Value); // Safe with copy, dangling reference otherwise.
80+
}

0 commit comments

Comments
 (0)