[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 05:31:35 PST 2026
================
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "RedundantStringViewConversionsCheck.h"
+#include "clang/AST/Expr.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+static auto getStringTypeMatcher(StringRef CharType) {
+ return hasCanonicalType(hasDeclaration(cxxRecordDecl(hasName(CharType))));
+}
+
+void RedundantStringViewConversionsCheck::registerMatchers(
+ MatchFinder *Finder) {
+ const auto IsStdString = getStringTypeMatcher("::std::basic_string");
+ const auto IsStdStringView = getStringTypeMatcher("::std::basic_string_view");
+
+ const auto ImplicitlyConvertibleToStringView =
+ expr(anyOf(hasType(IsStdStringView), stringLiteral(),
+ hasType(pointerType(pointee(isAnyCharacter())))))
+ .bind("originalStringView");
+
+ Finder->addMatcher(
+ callExpr(forEachArgumentWithParam(
+ expr(hasType(IsStdStringView),
+ hasDescendant(
+ cxxFunctionalCastExpr(
+ hasType(IsStdString),
+ hasDescendant(cxxConstructExpr(
+ hasType(IsStdString),
+ hasArgument(0,
+ ignoringImplicit(
+ ImplicitlyConvertibleToStringView)),
+ unless(hasDeclaration(
+ cxxConstructorDecl(isCopyConstructor()))),
----------------
irishrover wrote:
Done!
https://github.com/llvm/llvm-project/pull/174288
More information about the cfe-commits
mailing list