[clang-tools-extra] [clang-tidy] Add a new check 'replace-with-string-view' (PR #172170)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 15 09:31:52 PST 2025
================
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_REPLACEWITHSTRINGVIEWCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_REPLACEWITHSTRINGVIEWCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::performance {
+
+/// Looks for functions returning `std::[w|u8|u16|u32]string` and suggests to
+/// change it to `std::[...]string_view` if possible and profitable.
+///
+/// Example:
+///
+/// std::string foo(int i) { // <---- can be replaced to std::string_view f(...)
+/// switch(i) {
+/// case 1:
+/// return "case1";
+/// case 2:
+/// return "case2";
+/// default:
+/// return {};
+/// }
+/// }
+///
----------------
vbvictor wrote:
```suggestion
```
https://github.com/llvm/llvm-project/pull/172170
More information about the cfe-commits
mailing list