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

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 16:00:26 PST 2024


================
@@ -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(
----------------
5chmidti wrote:

Please add `unless(isInTemplateInstantiation` as the first thing inside `cxxOperatorCallExpr` to exclude matches from inside template instantiations. We don't care about instantiations, only template declarations, because the instantiation may diagnose an issue from a substituted type, that is not a `string_view` in another instantiation.

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


More information about the cfe-commits mailing list