Skip to content

Commit e856483

Browse files
authored
[clang-tidy][doc] add more information in twine-local's document (#166266)
explain more about use-after-free in llvm-twine-local add note about manually adjusting code after applying fix-it. fixed: #154810
1 parent fedd3b0 commit e856483

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ should be generally avoided.
1414
// becomes
1515

1616
static std::string Moo = (Twine("bark") + "bah").str();
17+
18+
The ``Twine`` does not own the memory of its contents, so it is not
19+
recommended to use ``Twine`` created from temporary strings or string literals.
20+
21+
.. code-block:: c++
22+
23+
static Twine getModuleIdentifier(StringRef moduleName) {
24+
return moduleName + "_module";
25+
}
26+
void foo() {
27+
Twine result = getModuleIdentifier(std::string{"abc"} + "def");
28+
// temporary std::string is destroyed here, result is dangling
29+
}
30+
31+
After applying this fix-it hints, the code will use ``std::string`` instead of
32+
``Twine`` for local variables. However, ``Twine`` has lots of methods that
33+
are incompatible with ``std::string``, so the user may need to adjust the code
34+
manually after applying the fix-it hints.

0 commit comments

Comments
 (0)