[clang-tools-extra] 1fdb3e3 - abseil-string-find-str-contains should not propose an edit for the three-parameter version of find().

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 10 09:43:55 PDT 2021


Author: Tom Lokovic
Date: 2021-08-10T16:39:17Z
New Revision: 1fdb3e36ff379e5b3b05a00d49b6081435df727a

URL: https://github.com/llvm/llvm-project/commit/1fdb3e36ff379e5b3b05a00d49b6081435df727a
DIFF: https://github.com/llvm/llvm-project/commit/1fdb3e36ff379e5b3b05a00d49b6081435df727a.diff

LOG: abseil-string-find-str-contains should not propose an edit for the three-parameter version of find().

std::string, std::string_view, and absl::string_view all have a three-parameter version of find()
which has a "count" (or "n") paremeter limiting the size of the substring to search.  We don't want
to propose changing to absl::StrContains in those cases.  This change fixes that and adds unit tests
to confirm.

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D107837

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp b/clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
index f4a519c15b92f..601b987d332b6 100644
--- a/clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/StringFindStrContainsCheck.cpp
@@ -53,7 +53,7 @@ makeRewriteRule(const std::vector<std::string> &StringLikeClassNames,
       to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass))));
   auto StringFind = cxxMemberCallExpr(
       callee(cxxMethodDecl(
-          hasName("find"),
+          hasName("find"), parameterCountIs(2),
           hasParameter(
               0, parmVarDecl(anyOf(hasType(StringType), hasType(CharStarType),
                                    hasType(CharType)))))),

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp b/clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
index 81a3fc4d3b971..ba9a72a60bab0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-str-contains.cpp
@@ -15,6 +15,7 @@ class basic_string {
   ~basic_string();
   int find(basic_string s, int pos = 0);
   int find(const C *s, int pos = 0);
+  int find(const C *s, int pos, int n);
   int find(char c, int pos = 0);
   static constexpr size_t npos = -1;
 };
@@ -30,6 +31,7 @@ class basic_string_view {
   ~basic_string_view();
   int find(basic_string_view s, int pos = 0);
   int find(const C *s, int pos = 0);
+  int find(const C *s, int pos, int n);
   int find(char c, int pos = 0);
   static constexpr size_t npos = -1;
 };
@@ -48,6 +50,7 @@ class string_view {
   ~string_view();
   int find(string_view s, int pos = 0);
   int find(const char *s, int pos = 0);
+  int find(const char *s, int pos, int n);
   int find(char c, int pos = 0);
   static constexpr size_t npos = -1;
 };
@@ -263,6 +266,18 @@ void no_nonzero_pos() {
   asv.find("a", 3) == std::string_view::npos;
 }
 
+// Confirms that it does not match when the count parameter is present.
+void no_count() {
+  std::string ss;
+  ss.find("a", 0, 1) == std::string::npos;
+
+  std::string_view ssv;
+  ssv.find("a", 0, 1) == std::string_view::npos;
+
+  absl::string_view asv;
+  asv.find("a", 0, 1) == std::string_view::npos;
+}
+
 // Confirms that it does not match when it's compared to something other than
 // npos, even if the value is the same as npos.
 void no_non_npos() {


        


More information about the cfe-commits mailing list