[clang-tools-extra] [clang-tidy] Add a new check 'replace-with-string-view' (PR #172170)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 13 18:17:31 PST 2025


================
@@ -0,0 +1,60 @@
+.. title:: clang-tidy - performance-replace-with-string-view
+
+performance-replace-with-string-view
+====================================
+
+Looks for functions returning `std::[w|u8|u16|u32]string` and suggests to
+change it to `std::[...]string_view` for performance reasons.
+
+Rationale:
+
+Each time a new ``std::string`` is created from a literal, a copy of that
+literal is allocated either in ``std::string``'s internal buffer
+(for short literals) or in a heap.
+
+For the cases where ``std::string`` is returned from a function,
+such allocations can be eliminated sometimes by using std::string_view
----------------
EugeneZelenko wrote:

```suggestion
such allocations can be eliminated sometimes by using ``std::string_view``
```

https://github.com/llvm/llvm-project/pull/172170


More information about the cfe-commits mailing list