[clang-tools-extra] [clang-tidy] Add readability-string-view-substr check (PR #120055)

Helmut Januschka via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 7 14:56:04 PST 2025


================
@@ -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) {
----------------
hjanuschka wrote:

all other feedback addressed, and pushed, will go and kind of mentally restart this, to get the most logic moved to check() 🙈

https://github.com/llvm/llvm-project/pull/120055


More information about the cfe-commits mailing list