niko created this revision.
niko added a reviewer: hokein.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

string::find() has a 3-argument version, with the third argument
specifying the 'length of sequence of characters to match' (for a const
char* 1st argument, typically used when it is not null-terminated).

This case seems rare enough that I think the best way is to just ignore
it. I believe the second-best alternative would be to pull in
string_view.h, and suggest constructing string_view from the first and
third argument - this seems like overkill but i'm happy to be convinced
otherwise.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76496

Files:
  clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
@@ -13,6 +13,7 @@
   ~basic_string();
   int find(basic_string<C> s, int pos = 0);
   int find(const char *s, int pos = 0);
+  int find(const char *s, int pos, int n);
 };
 typedef basic_string<char> string;
 typedef basic_string<wchar_t> wstring;
@@ -41,6 +42,10 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
   // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, s);{{$}}
 
+  s.find(s, 0) == 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
+  // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, s);{{$}}
+
   s.find("aaa") != 0;
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, "aaa");{{$}}
@@ -66,4 +71,5 @@
   s.find("a", 1) == 0;
   s.find("a", 1) == 1;
   s.find("a") == 1;
+  s.find("a", 0, 1) == 0;
 }
Index: clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -40,8 +40,9 @@
 
   auto StringFind = cxxMemberCallExpr(
       // .find()-call on a string...
-      callee(cxxMethodDecl(hasName("find"))),
-      on(hasType(StringType)),
+      callee(cxxMethodDecl(hasName("find"))), on(hasType(StringType)),
+      // ... with 1 or two arguments (but not three),
+      anyOf(argumentCountIs(1), argumentCountIs(2)),
       // ... with some search expression ...
       hasArgument(0, expr().bind("needle")),
       // ... and either "0" as second argument or the default argument (also 
0).


Index: clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/abseil-string-find-startswith.cpp
@@ -13,6 +13,7 @@
   ~basic_string();
   int find(basic_string<C> s, int pos = 0);
   int find(const char *s, int pos = 0);
+  int find(const char *s, int pos, int n);
 };
 typedef basic_string<char> string;
 typedef basic_string<wchar_t> wstring;
@@ -41,6 +42,10 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
   // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, s);{{$}}
 
+  s.find(s, 0) == 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
+  // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, s);{{$}}
+
   s.find("aaa") != 0;
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
   // CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, "aaa");{{$}}
@@ -66,4 +71,5 @@
   s.find("a", 1) == 0;
   s.find("a", 1) == 1;
   s.find("a") == 1;
+  s.find("a", 0, 1) == 0;
 }
Index: clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -40,8 +40,9 @@
 
   auto StringFind = cxxMemberCallExpr(
       // .find()-call on a string...
-      callee(cxxMethodDecl(hasName("find"))),
-      on(hasType(StringType)),
+      callee(cxxMethodDecl(hasName("find"))), on(hasType(StringType)),
+      // ... with 1 or two arguments (but not three),
+      anyOf(argumentCountIs(1), argumentCountIs(2)),
       // ... with some search expression ...
       hasArgument(0, expr().bind("needle")),
       // ... and either "0" as second argument or the default argument (also 0).
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to