Skip to content

Commit 2aec866

Browse files
nicovankPiotrZSL
authored andcommitted
[clang-tidy][abseil-string-find-startswith] Add string_view to default string-like classes (#72283)
As per title. A small improvement to this check, `string_view` should work out of the box.
1 parent dae3c44 commit 2aec866

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ using namespace clang::ast_matchers;
1919

2020
namespace clang::tidy::abseil {
2121

22+
const auto DefaultStringLikeClasses =
23+
"::std::basic_string;::std::basic_string_view";
24+
2225
StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,
2326
ClangTidyContext *Context)
2427
: ClangTidyCheck(Name, Context),
2528
StringLikeClasses(utils::options::parseStringList(
26-
Options.get("StringLikeClasses", "::std::basic_string"))),
29+
Options.get("StringLikeClasses", DefaultStringLikeClasses))),
2730
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
2831
utils::IncludeSorter::IS_LLVM),
2932
areDiagsSelfContained()),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ New check aliases
208208
Changes in existing checks
209209
^^^^^^^^^^^^^^^^^^^^^^^^^^
210210

211+
- Improved :doc:`abseil-string-find-startswith
212+
<clang-tidy/checks/abseil/string-find-startswith>` check to also consider
213+
``std::basic_string_view`` in addition to ``std::basic_string`` by default.
214+
211215
- Improved :doc:`bugprone-dangling-handle
212216
<clang-tidy/checks/bugprone/dangling-handle>` check to support functional
213217
casting during type conversions at variable initialization, now with improved

clang-tools-extra/docs/clang-tidy/checks/abseil/string-find-startswith.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
abseil-string-find-startswith
44
=============================
55

6-
Checks whether a ``std::string::find()`` or ``std::string::rfind()`` result is
7-
compared with 0, and suggests replacing with ``absl::StartsWith()``. This is
8-
both a readability and performance issue.
6+
Checks whether a ``std::string::find()`` or ``std::string::rfind()`` (and
7+
corresponding ``std::string_view`` methods) result is compared with 0, and
8+
suggests replacing with ``absl::StartsWith()``. This is both a readability and
9+
performance issue.
910

1011
.. code-block:: c++
1112

@@ -28,9 +29,9 @@ Options
2829

2930
.. option:: StringLikeClasses
3031

31-
Semicolon-separated list of names of string-like classes. By default only
32-
``std::basic_string`` is considered. The list of methods to considered is
33-
fixed.
32+
Semicolon-separated list of names of string-like classes. By default both
33+
``std::basic_string`` and ``std::basic_string_view`` are considered. The list
34+
of methods to be considered is fixed.
3435

3536
.. option:: IncludeStyle
3637

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ struct basic_string {
4343
size_type find(const C* s, size_type pos = 0) const;
4444
size_type find(const C* s, size_type pos, size_type n) const;
4545

46+
size_type rfind(const _Type& str, size_type pos = npos) const;
47+
size_type rfind(const C* s, size_type pos, size_type count) const;
48+
size_type rfind(const C* s, size_type pos = npos) const;
49+
size_type rfind(C ch, size_type pos = npos) const;
50+
4651
_Type& insert(size_type pos, const _Type& str);
4752
_Type& insert(size_type pos, const C* s);
4853
_Type& insert(size_type pos, const C* s, size_type n);
@@ -54,6 +59,8 @@ struct basic_string {
5459
_Type& operator+=(const C* s);
5560
_Type& operator=(const _Type& str);
5661
_Type& operator=(const C* s);
62+
63+
static constexpr size_t npos = -1;
5764
};
5865

5966
typedef basic_string<char> string;
@@ -63,8 +70,23 @@ typedef basic_string<char32> u32string;
6370

6471
template <typename C, typename T = char_traits<C>>
6572
struct basic_string_view {
73+
typedef size_t size_type;
74+
typedef basic_string_view<C, T> _Type;
75+
6676
const C *str;
6777
constexpr basic_string_view(const C* s) : str(s) {}
78+
79+
size_type find(_Type v, size_type pos = 0) const;
80+
size_type find(C ch, size_type pos = 0) const;
81+
size_type find(const C* s, size_type pos, size_type count) const;
82+
size_type find(const C* s, size_type pos = 0) const;
83+
84+
size_type rfind(_Type v, size_type pos = npos) const;
85+
size_type rfind(C ch, size_type pos = npos) const;
86+
size_type rfind(const C* s, size_type pos, size_type count) const;
87+
size_type rfind(const C* s, size_type pos = npos) const;
88+
89+
static constexpr size_t npos = -1;
6890
};
6991
typedef basic_string_view<char> string_view;
7092
typedef basic_string_view<wchar_t> wstring_view;

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
// RUN: %check_clang_tidy %s abseil-string-find-startswith %t -- \
2-
// RUN: -config="{CheckOptions: {abseil-string-find-startswith.StringLikeClasses: '::std::basic_string;::basic_string'}}"
2+
// RUN: -config="{CheckOptions: \
3+
// RUN: {abseil-string-find-startswith.StringLikeClasses: \
4+
// RUN: '::std::basic_string;::std::basic_string_view;::basic_string'}}" \
5+
// RUN: -- -isystem %clang_tidy_headers
6+
7+
#include <string>
38

49
using size_t = decltype(sizeof(int));
510

611
namespace std {
7-
template <typename T> class allocator {};
8-
template <typename T> class char_traits {};
9-
template <typename C, typename T = std::char_traits<C>,
10-
typename A = std::allocator<C>>
11-
struct basic_string {
12-
basic_string();
13-
basic_string(const basic_string &);
14-
basic_string(const C *, const A &a = A());
15-
~basic_string();
16-
int find(basic_string<C> s, int pos = 0);
17-
int find(const char *s, int pos = 0);
18-
int rfind(basic_string<C> s, int pos = npos);
19-
int rfind(const char *s, int pos = npos);
20-
static constexpr size_t npos = -1;
21-
};
22-
typedef basic_string<char> string;
23-
typedef basic_string<wchar_t> wstring;
24-
2512
struct cxx_string {
2613
int find(const char *s, int pos = 0);
2714
int rfind(const char *s, int pos = npos);
@@ -39,7 +26,7 @@ std::string bar();
3926

4027
#define A_MACRO(x, y) ((x) == (y))
4128

42-
void tests(std::string s, global_string s2) {
29+
void tests(std::string s, global_string s2, std::string_view sv) {
4330
s.find("a") == 0;
4431
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith instead of find() == 0 [abseil-string-find-startswith]
4532
// CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, "a");{{$}}
@@ -96,6 +83,14 @@ void tests(std::string s, global_string s2) {
9683
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
9784
// CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s2, "a");{{$}}
9885

86+
sv.find("a") == 0;
87+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
88+
// CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(sv, "a");{{$}}
89+
90+
sv.rfind("a", 0) != 0;
91+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
92+
// CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(sv, "a");{{$}}
93+
9994
// expressions that don't trigger the check are here.
10095
A_MACRO(s.find("a"), 0);
10196
A_MACRO(s.rfind("a", 0), 0);

0 commit comments

Comments
 (0)