================ @@ -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()))), ---------------- zeyi2 wrote:
I think this may introduce a potential FN? ```c++ std::string s = "hello"; void f(std::string_view) f(std::string(s)); ``` `std::string(s)` creates a redundant temporary copy. So maybe we need to include the copy constructor case as well. https://github.com/llvm/llvm-project/pull/174288 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
