-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] support string::contains #110351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b98e9a4
e42c356
9d44ee0
3625221
608e883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -50,12 +50,16 @@ struct basic_string { | |||||||||||
size_type find(const _Type& str, size_type pos = 0) const; | ||||||||||||
size_type find(const C* s, size_type pos = 0) const; | ||||||||||||
size_type find(const C* s, size_type pos, size_type n) const; | ||||||||||||
size_type find(C ch, size_type pos = 0) const; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's cover them all if possible. |
||||||||||||
|
||||||||||||
size_type rfind(const _Type& str, size_type pos = npos) const; | ||||||||||||
size_type rfind(const C* s, size_type pos, size_type count) const; | ||||||||||||
size_type rfind(const C* s, size_type pos = npos) const; | ||||||||||||
size_type rfind(C ch, size_type pos = npos) const; | ||||||||||||
|
||||||||||||
bool contains(const C *s) const; | ||||||||||||
bool contains(C s) const; | ||||||||||||
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
_Type& insert(size_type pos, const _Type& str); | ||||||||||||
_Type& insert(size_type pos, const C* s); | ||||||||||||
_Type& insert(size_type pos, const C* s, size_type n); | ||||||||||||
|
@@ -104,6 +108,9 @@ struct basic_string_view { | |||||||||||
size_type rfind(const C* s, size_type pos, size_type count) const; | ||||||||||||
size_type rfind(const C* s, size_type pos = npos) const; | ||||||||||||
|
||||||||||||
bool contains(const C *s) const; | ||||||||||||
bool contains(C s) const; | ||||||||||||
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
constexpr bool starts_with(basic_string_view sv) const noexcept; | ||||||||||||
constexpr bool starts_with(C ch) const noexcept; | ||||||||||||
constexpr bool starts_with(const C* s) const; | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,7 @@ | ||||||
// RUN: %check_clang_tidy -std=c++20-or-later %s readability-container-contains %t | ||||||
// RUN: %check_clang_tidy -std=c++20-or-later %s readability-container-contains %t -- \ | ||||||
// RUN: -- -isystem %clang_tidy_headers | ||||||
|
||||||
#include <string> | ||||||
|
||||||
// Some *very* simplified versions of `map` etc. | ||||||
namespace std { | ||||||
|
@@ -29,6 +32,8 @@ struct multimap { | |||||
bool contains(const Key &K) const; | ||||||
}; | ||||||
|
||||||
|
||||||
|
||||||
} // namespace std | ||||||
|
||||||
// Check that we detect various common ways to check for membership | ||||||
|
@@ -453,3 +458,29 @@ void testOperandPermutations(std::map<int, int>& Map) { | |||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (!Map.contains(0)) {}; | ||||||
} | ||||||
|
||||||
void testStringNops(std::string Str, std::string SubStr, std::string_view StrView) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just a typo. |
||||||
if (Str.find("test") == std::string::npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (!Str.contains("test")) {}; | ||||||
|
||||||
if (Str.find('c') != std::string::npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (Str.contains('c')) {}; | ||||||
|
||||||
if (Str.find('c') != Str.npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (Str.contains('c')) {}; | ||||||
|
||||||
if (StrView.find("test") == std::string::npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (!StrView.contains("test")) {}; | ||||||
|
||||||
if (StrView.find('c') != std::string::npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (StrView.contains('c')) {}; | ||||||
|
||||||
if (StrView.find('c') != StrView.npos) {}; | ||||||
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains] | ||||||
// CHECK-FIXES: if (StrView.contains('c')) {}; | ||||||
} | ||||||
dl8sd11 marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a little more detail is good here.