================
@@ -0,0 +1,201 @@
+//===--- StringViewSubstrCheck.cpp - clang-tidy------------------*- C++ 
-*-===//
+//
+// 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 "StringViewSubstrCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+void StringViewSubstrCheck::registerMatchers(MatchFinder *Finder) {
+  const auto HasStringViewType = 
hasType(hasUnqualifiedDesugaredType(recordType(
+      hasDeclaration(recordDecl(hasName("::std::basic_string_view"))))));
+
+  // Match assignment to string_view's substr
+  Finder->addMatcher(
+      cxxOperatorCallExpr(
+          hasOverloadedOperatorName("="),
+          hasArgument(0, expr(HasStringViewType).bind("target")),
+          hasArgument(
+              1, cxxMemberCallExpr(callee(memberExpr(hasDeclaration(
+                                       cxxMethodDecl(hasName("substr"))))),
+                                   on(expr(HasStringViewType).bind("source")))
+                     .bind("substr_call")))
+          .bind("assignment"),
+      this);
+}
+
+void StringViewSubstrCheck::check(const MatchFinder::MatchResult &Result) {
----------------
5chmidti wrote:

Please try to do as much of the checking inside the matchers. I think 
everything can be done inside the matcher.

https://github.com/llvm/llvm-project/pull/120055
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to