[clang-tools-extra] [clang-tidy] Add readability-stringview-substr check (PR #120055)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 21 03:34:08 PST 2026
================
@@ -0,0 +1,192 @@
+//===----------------------------------------------------------------------===//
+//
+// 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) {
+ // Match string_view type
+ const auto StringViewDecl = recordDecl(hasName("::std::basic_string_view"));
+ const auto IsStringView = qualType(
+ hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringViewDecl))));
+
+ // Length/size matcher reused in multiple places
+ const auto LengthMatcher = cxxMemberCallExpr(callee(memberExpr(hasDeclaration(
+ cxxMethodDecl(anyOf(hasName("length"), hasName("size")))))));
+
+ // Match various forms of zero
+ const auto IsZero =
+ expr(anyOf(ignoringParenImpCasts(integerLiteral(equals(0))),
----------------
zeyi2 wrote:
I'm not sure about `ignoringParenImpCasts`, IMHO it may fail to match expressions like `(size_t)0`?
Would like to hear the opinions from other reviewers :)
https://github.com/llvm/llvm-project/pull/120055
More information about the cfe-commits
mailing list