tdl-g created this revision. tdl-g added a reviewer: ymandel. tdl-g requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D107837 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 @@ -15,6 +15,7 @@ ~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 @@ ~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 @@ ~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 @@ 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() { 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 @@ -53,7 +53,7 @@ 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)))))),
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 @@ -15,6 +15,7 @@ ~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 @@ ~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 @@ ~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 @@ 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() { 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 @@ -53,7 +53,7 @@ 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)))))),
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits