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

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 18 06:49:51 PST 2025


================
@@ -0,0 +1,209 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s performance-replace-with-string-view %t -- -- -isystem %clang_tidy_headers
+
+#include <string>
+
+namespace std::literals {
+  namespace string_literals {
+    string operator""s(const char *, size_t);
+  }
+  namespace string_view_literals {
+    string_view operator""sv(const char *, size_t);
+  }
+}
+
+// ==========================================================
+// Positive tests
+// ==========================================================
+
+std::string simpleLiteral() {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view simpleLiteral() {
+  return "simpleLiteral";
+}
+
+std::string emptyReturn() {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view emptyReturn() {
+  return {};
+}
+
+std::string ctorReturn() {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view ctorReturn() {
+  return std::string();
+}
+
+std::string switchCaseTest(int i) {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view switchCaseTest(int i) {
+  switch (i) {
+  case 1:
+    return "case1";
+  case 2:
+    return "case2";
+  case 3:
+    return {};
+  default:
+    return "default";
+  }
+}
+
+std::string ifElseTest(bool flag) {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view ifElseTest(bool flag) {
+  if (flag)
+    return "true";
+  return "false";
+}
+
+std::wstring wideStringTest() {
+// CHECK-MESSAGES:[[@LINE-1]]:1: warning: consider using 'std::wstring_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::wstring_view wideStringTest() {
+  return L"wide literal";
+}
+
+class A {
+  std::string classMethodInt() { return "internal"; }
+// CHECK-MESSAGES:[[@LINE-1]]:3: warning: consider using 'std::string_view' to avoid unnecessary copying and allocations [performance-replace-with-string-view]
+// CHECK-FIXES: std::string_view classMethodInt() { return "internal"; }
+
+  std::string classMethodExt();
+// CHECK-FIXES: std::string_view classMethodExt();
----------------
irishrover wrote:

Correct. The message is reported only once. But fix-hints are reported for each item from MatchedDecl->redecls().

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


More information about the cfe-commits mailing list