[PATCH] D92810: [clang-tidy] Recognize single character needles for absl::StrContains.
Chris Kennelly via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 7 20:31:49 PST 2020
ckennelly created this revision.
ckennelly added reviewers: EricWF, ymandel, hokein.
Herald added a subscriber: xazax.hun.
ckennelly requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Commit fbdff6f3ae0b in the Abseil tree adds an overload for
absl::StrContains to accept a single character needle for optimized
lookups.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D92810
Files:
clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -226,17 +226,21 @@
// CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, cc);{{$}}
}
-// Confirms that it does *not* match when the parameter to find() is a char,
-// because absl::StrContains is not implemented for char.
-void no_char_param_tests() {
+void char_param_tests() {
std::string ss;
ss.find('c') == std::string::npos;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+ // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ss, 'c');{{$}}
std::string_view ssv;
ssv.find('c') == std::string_view::npos;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+ // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(ssv, 'c');{{$}}
absl::string_view asv;
asv.find('c') == absl::string_view::npos;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StrContains instead of
+ // CHECK-FIXES: {{^[[:space:]]*}}!absl::StrContains(asv, 'c');{{$}}
}
#define FOO(a, b, c, d) ((a).find(b) == std::string::npos ? (c) : (d))
Index: clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
@@ -31,6 +31,8 @@
using ::clang::transformer::node;
using ::clang::transformer::RewriteRule;
+AST_MATCHER(Type, isCharType) { return Node.isCharType(); }
+
static const char DefaultStringLikeClasses[] = "::std::basic_string;"
"::std::basic_string_view;"
"::absl::string_view";
@@ -58,13 +60,15 @@
hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
auto CharStarType =
hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter())));
+ auto CharType = hasUnqualifiedDesugaredType(isCharType());
auto StringNpos = declRefExpr(
to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass))));
auto StringFind = cxxMemberCallExpr(
callee(cxxMethodDecl(
hasName("find"),
- hasParameter(0, parmVarDecl(anyOf(hasType(StringType),
- hasType(CharStarType)))))),
+ hasParameter(
+ 0, parmVarDecl(anyOf(hasType(StringType), hasType(CharStarType),
+ hasType(CharType)))))),
on(hasType(StringType)), hasArgument(0, expr().bind("parameter_to_find")),
anyOf(hasArgument(1, integerLiteral(equals(0))),
hasArgument(1, cxxDefaultArgExpr())),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92810.310081.patch
Type: text/x-patch
Size: 2963 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201208/7c134041/attachment-0001.bin>
More information about the cfe-commits
mailing list