[clang-tools-extra] [clang-tidy] Add a new check 'performance-string-view-conversions' (PR #174288)
Zinovy Nis via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 9 01:12:43 PST 2026
https://github.com/irishrover updated https://github.com/llvm/llvm-project/pull/174288
>From f3ae3a6f57a23ebe1e9a7ca2273fd9d3705eaedb Mon Sep 17 00:00:00 2001
From: Zinovy Nis <zinovy.nis at gmail.com>
Date: Sat, 3 Jan 2026 17:48:40 +0300
Subject: [PATCH] [clang-tidy] Add a new check
'performance-string-view-conversions'
Finds and removes redundant conversions from ``std::[w|u8|u16|u32]string_view``
to ``std::[...]string`` in call expressions expecting ``std::[...]string_view``.
---
.../clang-tidy/performance/CMakeLists.txt | 1 +
.../performance/PerformanceTidyModule.cpp | 3 +
.../StringViewConversionsCheck.cpp | 149 ++++++++++++++++++
.../performance/StringViewConversionsCheck.h | 34 ++++
clang-tools-extra/docs/ReleaseNotes.rst | 6 +
.../docs/clang-tidy/checks/list.rst | 1 +
.../performance/string-view-conversions.rst | 32 ++++
.../clang-tidy/checkers/Inputs/Headers/string | 24 +++
.../string-view-conversions-cxx20.cpp | 27 ++++
.../performance/string-view-conversions.cpp | 89 +++++++++++
10 files changed, 366 insertions(+)
create mode 100644 clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp
create mode 100644 clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h
create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp
diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt
index 9a2f90069edbf..4dba117e1ee54 100644
--- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt
@@ -21,6 +21,7 @@ add_clang_library(clangTidyPerformanceModule STATIC
NoexceptMoveConstructorCheck.cpp
NoexceptSwapCheck.cpp
PerformanceTidyModule.cpp
+ StringViewConversionsCheck.cpp
TriviallyDestructibleCheck.cpp
TypePromotionInMathFnCheck.cpp
UnnecessaryCopyInitializationCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
index 6bab1a46d18db..294a209e4c602 100644
--- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
@@ -23,6 +23,7 @@
#include "NoexceptDestructorCheck.h"
#include "NoexceptMoveConstructorCheck.h"
#include "NoexceptSwapCheck.h"
+#include "StringViewConversionsCheck.h"
#include "TriviallyDestructibleCheck.h"
#include "TypePromotionInMathFnCheck.h"
#include "UnnecessaryCopyInitializationCheck.h"
@@ -62,6 +63,8 @@ class PerformanceModule : public ClangTidyModule {
"performance-noexcept-move-constructor");
CheckFactories.registerCheck<NoexceptSwapCheck>(
"performance-noexcept-swap");
+ CheckFactories.registerCheck<StringViewConversionsCheck>(
+ "performance-string-view-conversions");
CheckFactories.registerCheck<TriviallyDestructibleCheck>(
"performance-trivially-destructible");
CheckFactories.registerCheck<TypePromotionInMathFnCheck>(
diff --git a/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp
new file mode 100644
index 0000000000000..b93d3c43c610f
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp
@@ -0,0 +1,149 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "StringViewConversionsCheck.h"
+#include "clang/AST/Expr.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+static auto getStringTypeMatcher(StringRef CharType) {
+ return hasCanonicalType(hasDeclaration(cxxRecordDecl(hasName(CharType))));
+}
+
+void StringViewConversionsCheck::registerMatchers(MatchFinder *Finder) {
+ // Matchers for std::basic_[w|u8|u16|u32]string and
+ // std::basic_[w|u8|u16|u32]string_view families.
+ const auto IsStdString = getStringTypeMatcher("::std::basic_string");
+ const auto IsStdStringView = getStringTypeMatcher("::std::basic_string_view");
+
+ // Matches pointer to any character type (char*, const char*, wchar_t*, etc.)
+ // or array of any character type (char[], char[N], const char[N], etc.).
+ const auto IsCharPointerOrArray =
+ anyOf(hasType(pointerType(pointee(isAnyCharacter()))),
+ hasType(arrayType(hasElementType(isAnyCharacter()))));
+
+ // Matches expressions that can be implicitly converted to string_view
+ // without going through std::string:
+ // - string_view itself (no conversion needed)
+ // - string literals ("hello", L"wide", u8"utf8", etc.)
+ // - character pointers (const char*, char*)
+ // - character arrays (char arr[], char arr[N])
+ // These are the expressions we want to preserve after removing
+ // the redundant std::string conversion.
+ const auto ImplicitlyConvertibleToStringView =
+ expr(anyOf(hasType(IsStdStringView), stringLiteral(),
+ IsCharPointerOrArray))
+ .bind("originalStringView");
+
+ // Matches direct std::string construction from a string_view-convertible
+ // expression. This handles cases like:
+ // - Implicit construction in certain contexts
+ // - Brace initialization: std::string{sv}
+ // Excludes copy/move constructors to avoid false positives when
+ // an existing std::string is being copied or moved.
+ const auto RedundantStringConstruction = cxxConstructExpr(
+ hasType(IsStdString),
+ hasArgument(0, ignoringImplicit(ImplicitlyConvertibleToStringView)));
+
+ // Matches functional cast syntax: std::string(expr)
+ // In the AST, this appears as CXXFunctionalCastExpr containing
+ // a CXXConstructExpr. Example: std::string(sv), std::string("literal")
+ const auto RedundantFunctionalCast = cxxFunctionalCastExpr(
+ hasType(IsStdString), hasDescendant(RedundantStringConstruction));
+
+ // Match method calls on std::string that modify or use the string,
+ // such as operator+, append(), substr(), c_str(), etc.
+ // When these are present, the std::string construction is not redundant.
+ const auto HasStringOperatorCall = hasDescendant(cxxOperatorCallExpr(
+ hasOverloadedOperatorName("+"), hasType(IsStdString)));
+ const auto HasStringMethodCall =
+ hasDescendant(cxxMemberCallExpr(on(hasType(IsStdString))));
+
+ // Main matcher: finds function calls where:
+ // 1. A parameter has type string_view
+ // 2. The corresponding argument contains a redundant std::string construction
+ // (either functional cast syntax or direct construction/brace init)
+ // 3. The argument does NOT involve:
+ // - String concatenation with operator+ (string_view doesn't support it)
+ // - Method calls on the std::string (like append(), substr(), etc.)
+ //
+ // Detected patterns (will be flagged):
+ // void foo(std::string_view sv);
+ // foo(std::string(sv)); // sv -> string -> string_view
+ // foo(std::string{"literal"}); // literal -> string -> string_view
+ // foo(std::string(ptr)); // const char* -> string -> string_view
+ //
+ // Excluded patterns (will NOT be flagged):
+ // foo(std::string(sv) + "suffix"); // operator+ requires std::string
+ // foo("prefix" + std::string(sv)); // operator+ requires std::string
+ // foo(std::string("x").append("y")); // append() requires std::string
+ // foo(std::string(sv).substr(0, 5)); // substr() requires
+ Finder->addMatcher(
+ callExpr(
+ forEachArgumentWithParam(
+ expr(hasType(IsStdStringView),
+ // Match either syntax for std::string construction
+ hasDescendant(expr(anyOf(RedundantFunctionalCast,
+ RedundantStringConstruction))
+ .bind("redundantExpr")),
+ // Exclude cases of std::string methods or operator+ calls
+ unless(anyOf(HasStringOperatorCall, HasStringMethodCall)))
+ .bind("expr"),
+ parmVarDecl(hasType(IsStdStringView))))
+ .bind("call"),
+ this);
+}
+
+void StringViewConversionsCheck::check(const MatchFinder::MatchResult &Result) {
+ // Get the full argument expression passed to the function.
+ // This has type string_view after implicit conversions.
+ const auto *ParamExpr = Result.Nodes.getNodeAs<Expr>("expr");
+ if (!ParamExpr)
+ return;
+
+ // Get the redundant std::string construction expression.
+ // This is either CXXFunctionalCastExpr for std::string(x) syntax
+ // or CXXTemporaryObjectExpr for std::string{x} syntax.
+ const auto *RedundantExpr = Result.Nodes.getNodeAs<Expr>("redundantExpr");
+ if (!RedundantExpr)
+ return;
+
+ // Get the original expression that was passed to std::string constructor.
+ // This is what we want to use as the replacement.
+ const auto *OriginalExpr = Result.Nodes.getNodeAs<Expr>("originalStringView");
+ if (!OriginalExpr)
+ return;
+
+ // Sanity check. Verify that the redundant expression is the direct source of
+ // the argument, not part of a larger expression (e.g., std::string(sv) +
+ // "bar"). If source ranges don't match, there's something between the string
+ // construction and the function argument, so we shouldn't transform.
+ assert(ParamExpr->getSourceRange() == RedundantExpr->getSourceRange());
+
+ // Extract the source text of the original expression to use as replacement.
+ // For example, if the code is std::string(sv), we extract "sv".
+ const StringRef OriginalText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(OriginalExpr->getSourceRange()),
+ *Result.SourceManager, getLangOpts());
+
+ // Skip if we couldn't extract the source text (e.g., macro expansion issues).
+ if (OriginalText.empty())
+ return;
+
+ diag(RedundantExpr->getBeginLoc(),
+ "redundant conversion to %0 and then back to %1")
+ << RedundantExpr->getType() << ParamExpr->getType()
+ << FixItHint::CreateReplacement(RedundantExpr->getSourceRange(),
+ OriginalText);
+}
+
+} // namespace clang::tidy::performance
diff --git a/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h
new file mode 100644
index 0000000000000..d1d82a55fc9f0
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_STRINGVIEWCONVERSIONSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_STRINGVIEWCONVERSIONSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::performance {
+
+/// Finds and removes redundant conversions from std::string_view to std::string
+/// in call expressions expecting std::string_view.
+///
+/// For the user-facing documentation see:
+/// https://clang.llvm.org/extra/clang-tidy/checks/performance/string-view-conversions.html
+class StringViewConversionsCheck : public ClangTidyCheck {
+public:
+ StringViewConversionsCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+ return LangOpts.CPlusPlus17;
+ }
+};
+
+} // namespace clang::tidy::performance
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_STRINGVIEWCONVERSIONSCHECK_H
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 576aedaf427e6..1b1317fa16a08 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -265,6 +265,12 @@ New checks
Finds virtual function overrides with different visibility than the function
in the base class.
+- New :doc:`performance-string-view-conversions
+ <clang-tidy/checks/performance/string-view-conversions>` check.
+
+ Finds and removes redundant conversions from ``std::[w|u8|u16|u32]string_view`` to
+ ``std::[...]string`` in call expressions expecting ``std::[...]string_view``.
+
- New :doc:`readability-inconsistent-ifelse-braces
<clang-tidy/checks/readability/inconsistent-ifelse-braces>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index 3dabf887dc2e1..8fbf35bd18880 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -363,6 +363,7 @@ Clang-Tidy Checks
:doc:`performance-noexcept-destructor <performance/noexcept-destructor>`, "Yes"
:doc:`performance-noexcept-move-constructor <performance/noexcept-move-constructor>`, "Yes"
:doc:`performance-noexcept-swap <performance/noexcept-swap>`, "Yes"
+ :doc:`performance-string-view-conversions <performance/string-view-conversions>`, "Yes"
:doc:`performance-trivially-destructible <performance/trivially-destructible>`, "Yes"
:doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "Yes"
:doc:`performance-unnecessary-copy-initialization <performance/unnecessary-copy-initialization>`, "Yes"
diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst
new file mode 100644
index 0000000000000..347c3a7597689
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst
@@ -0,0 +1,32 @@
+.. title:: clang-tidy - performance-string-view-conversions
+
+performance-string-view-conversions
+===================================
+
+Finds and removes redundant conversions from ``std::[w|u8|u16|u32]string_view``
+to ``std::[...]string`` in call expressions expecting ``std::[...]string_view``.
+
+
+Before:
+
+.. code-block:: c++
+
+ void foo(int p1, std::string_view p2, double p3);
+ void bar(std::string_view sv) {
+ foo(42, std::string(sv), 3.14); // conversion to std::string is
+ // redundant as std::string_view
+ // is expected
+ foo(42, std::string("foo"), 3.14); // conversion to std::string is
+ // redundant as std::string_view
+ // is expected
+ }
+
+After:
+
+.. code-block:: c++
+
+ void foo(int p1, std::string_view p2, double p3);
+ void bar(std::string_view sv) {
+ foo(42, sv, 3.14);
+ foo(42, "foo", 3.14);
+ }
diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string
index 6cedda4202f14..c3fd2cf6c1ff7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string
+++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string
@@ -22,9 +22,11 @@ struct basic_string {
typedef size_t size_type;
typedef basic_string<C, T, A> _Type;
basic_string();
+ basic_string(basic_string_view<C, T>);
basic_string(const C *p, const A &a = A());
basic_string(const C *p, size_type count);
basic_string(const C *b, const C *e);
+ basic_string(size_t, C);
~basic_string();
@@ -90,8 +92,14 @@ struct basic_string {
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
+#if __cplusplus >= 202002L
+typedef basic_string<char8_t> u8string;
+typedef basic_string<char16_t> u16string;
+typedef basic_string<char32_t> u32string;
+#else
typedef basic_string<char16> u16string;
typedef basic_string<char32> u32string;
+#endif
template <typename C, typename T>
struct basic_string_view {
@@ -100,6 +108,7 @@ struct basic_string_view {
const C *str;
constexpr basic_string_view(const C* s) : str(s) {}
+ basic_string_view(const basic_string<C, T>&) {}
const C *data() const;
@@ -136,8 +145,14 @@ struct basic_string_view {
typedef basic_string_view<char> string_view;
typedef basic_string_view<wchar_t> wstring_view;
+#if __cplusplus >= 202002L
+typedef basic_string_view<char8_t> u8string_view;
+typedef basic_string_view<char16_t> u16string_view;
+typedef basic_string_view<char32_t> u32string_view;
+#else
typedef basic_string_view<char16> u16string_view;
typedef basic_string_view<char32> u32string_view;
+#endif
std::string operator+(const std::string&, const std::string&);
std::string operator+(const std::string&, const char*);
@@ -166,6 +181,15 @@ bool operator!=(const char*, const std::string_view&);
#endif
size_t strlen(const char* str);
+
+namespace literals {
+namespace string_literals {
+ string operator""s(const char *, size_t);
+}
+namespace string_view_literals {
+ string_view operator""sv(const char *, size_t);
+}
+}
}
#endif // _STRING_
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp
new file mode 100644
index 0000000000000..1a49883dad100
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s performance-string-view-conversions %t -- \
+// RUN: -- -isystem %clang_tidy_headers
+
+#include <string>
+
+using namespace std::literals::string_literals;
+using namespace std::literals::string_view_literals;
+
+void foo_u8sv(int p1, std::u8string_view p2, double p3);
+void foo_u16sv(int p1, std::u16string_view p2, double p3);
+void foo_u32sv(int p1, std::u32string_view p2, double p3);
+
+void positive(std::string_view sv, std::wstring_view wsv) {
+ // [u8|u16|32]string([u8|u16|32]string_view)
+ //
+ foo_u8sv(42, std::u8string(u8"Hello, world"), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant conversion to 'std::u8string' (aka 'basic_string<char8_t>') and then back to 'std::u8string_view' (aka 'basic_string_view<char8_t>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_u8sv(42, u8"Hello, world", 3.14);
+
+ foo_u16sv(42, std::u16string(u"Hello, world"), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant conversion to 'std::u16string' (aka 'basic_string<char16_t>') and then back to 'std::u16string_view' (aka 'basic_string_view<char16_t>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_u16sv(42, u"Hello, world", 3.14);
+
+ foo_u32sv(42, std::u32string(U"Hello, world"), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant conversion to 'std::u32string' (aka 'basic_string<char32_t>') and then back to 'std::u32string_view' (aka 'basic_string_view<char32_t>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_u32sv(42, U"Hello, world", 3.14);
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp
new file mode 100644
index 0000000000000..7c5610849da54
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp
@@ -0,0 +1,89 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s performance-string-view-conversions %t -- \
+// RUN: -- -isystem %clang_tidy_headers
+
+#include <string>
+
+using namespace std::literals::string_literals;
+using namespace std::literals::string_view_literals;
+
+void foo_sv(int p1, std::string_view p2, double p3);
+void foo_wsv(int p1, std::wstring_view p2, double p3);
+void foo_str(int p1, const std::string& p2, double p3);
+void foo_wstr(int p1, const std::wstring& p2, double p3);
+std::string foo_str(int p1);
+std::string_view foo_sv(int p1);
+
+void positive(std::string_view sv, std::wstring_view wsv) {
+ // string(string_view)
+ //
+ foo_sv(42, std::string(sv), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, sv, 3.14);
+
+ foo_sv(42, std::string("Hello, world"), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, "Hello, world", 3.14);
+
+ // TODO: support for ""sv literals
+ foo_sv(42, "Hello, world"s, 3.14);
+
+ foo_sv(42, std::string{"Hello, world"}, 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, "Hello, world", 3.14);
+
+ const char *ptr = "Hello, world";
+ foo_sv(42, std::string(ptr), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, ptr, 3.14);
+
+ char arr[] = "Hello, world";
+ foo_sv(42, std::string(arr), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, arr, 3.14);
+
+ foo_sv(42, std::string(foo_sv(42)), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to 'std::string' (aka 'basic_string<char>') and then back to 'std::string_view' (aka 'basic_string_view<char>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_sv(42, foo_sv(42), 3.14);
+
+ // wstring(wstring_view)
+ //
+ foo_wsv(42, std::wstring(wsv), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant conversion to 'std::wstring' (aka 'basic_string<wchar_t>') and then back to 'std::wstring_view' (aka 'basic_string_view<wchar_t>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_wsv(42, wsv, 3.14);
+
+ const wchar_t *wptr = L"Hello, world";
+ foo_wsv(42, std::wstring(wptr), 3.14);
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant conversion to 'std::wstring' (aka 'basic_string<wchar_t>') and then back to 'std::wstring_view' (aka 'basic_string_view<wchar_t>') [performance-string-view-conversions]
+ // CHECK-FIXES: foo_wsv(42, wptr, 3.14);
+}
+
+void negative(std::string_view sv, std::wstring_view wsv) {
+ // No warnings expected: already string_view
+ foo_sv(42, sv, 3.14);
+ foo_sv(42, "Hello, world", 3.14);
+ foo_sv(42, foo_sv(42), 3.14);
+ // No warnings expected: complex expression
+ foo_sv(42, std::string(sv) + "bar", 3.14);
+ foo_sv(42,
+ std::string( sv ) +
+ ("foo" "bar") ,
+ 3.14);
+ foo_sv(42, "foo" + std::string(sv), 3.14);
+ foo_sv(42, "foo" + std::string(sv) + "bar", 3.14);
+ foo_sv(42, std::string(sv) + std::string(sv), 3.14);
+ foo_sv(42, std::string("foo") + std::string("bar"), 3.14);
+ foo_sv(42, std::string(5, 'a'), 3.14);
+ foo_sv(42, std::string("foo").append("bar"), 3.14);
+ foo_sv(42, std::string(sv).substr(0, 5), 3.14);
+ foo_sv(42, std::string(sv).c_str(), 3.14);
+
+ // No warnings expected: string parameter, not string-view
+ foo_str(42, std::string(sv), 3.14);
+ foo_str(42, std::string("Hello, world"), 3.14);
+ foo_wstr(42, std::wstring(wsv), 3.14);
+ foo_wstr(42, std::wstring(L"Hello, world"), 3.14);
+
+ foo_sv(42, foo_str(42), 3.14);
+ foo_sv(42, foo_sv(42), 3.14);
+}
+
More information about the cfe-commits
mailing list