This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] add support to 3-arg find() in StringFindStartswith
AcceptedPublic

Authored by niko on Mar 20 2020, 7:06 AM.

Details

Summary

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 change adds support for this by wrapping the search string and the search string length in absl::string_view.

Diff Detail

Event Timeline

niko created this revision.Mar 20 2020, 7:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2020, 7:06 AM
Eugene.Zelenko added a project: Restricted Project.

I'm not hugely familiar with the abseil library, but from what I can see absl::StartsWith takes absl::string_view as args. Therefore surely it makes sense to handle the 3rd arg for str::find by explicitly constructing the absl::string_view from the pointer and size.

niko added a comment.Mar 27 2020, 3:03 PM

I'm not hugely familiar with the abseil library, but from what I can see absl::StartsWith takes absl::string_view as args. Therefore surely it makes sense to handle the 3rd arg for str::find by explicitly constructing the absl::string_view from the pointer and size.

I am mainly worried that the 3-arg find is rarely used, and using absl::string_view requires another option to be specified for the header file. If you think that's still a good tradeoff i'll change it to construct a string_view.

I'm not hugely familiar with the abseil library, but from what I can see absl::StartsWith takes absl::string_view as args. Therefore surely it makes sense to handle the 3rd arg for str::find by explicitly constructing the absl::string_view from the pointer and size.

I am mainly worried that the 3-arg find is rarely used, and using absl::string_view requires another option to be specified for the header file. If you think that's still a good tradeoff i'll change it to construct a string_view.

The absl/strings/match header includes the absl/strings/string_view header so you wouldn't need to include another header file. Well that's as long as the user hasn't done something funky with header files, but arguably the shouldn't be using Fix-Its if they have done something like that.

niko added a comment.Mar 30 2020, 7:03 AM

I'm not hugely familiar with the abseil library, but from what I can see absl::StartsWith takes absl::string_view as args. Therefore surely it makes sense to handle the 3rd arg for str::find by explicitly constructing the absl::string_view from the pointer and size.

I am mainly worried that the 3-arg find is rarely used, and using absl::string_view requires another option to be specified for the header file. If you think that's still a good tradeoff i'll change it to construct a string_view.

The absl/strings/match header includes the absl/strings/string_view header so you wouldn't need to include another header file. Well that's as long as the user hasn't done something funky with header files, but arguably the shouldn't be using Fix-Its if they have done something like that.

Correct me if I'm wrong, but that seems to be in violation of IWYU? Maybe I'm misreading this, or is the idea that higher-lever tooling (e.g. IWYU fixer) is supposed to address that?

Correct me if I'm wrong, but that seems to be in violation of IWYU? Maybe I'm misreading this, or is the idea that higher-lever tooling (e.g. IWYU fixer) is supposed to address that?

It is but it sort of gets a pass as the absl/strings/match.h has to include it.
Similar to how if you #include <vector>, you wouldn't then #include <initializer_list> so you could construct a std::vector using a std::initializer_list.

niko updated this revision to Diff 261512.May 1 2020, 11:39 AM

[clang-tidy] add 3-arg support to StringFindStartswith via string_view

niko retitled this revision from [clang-tidy] StringFindStartswith should ignore 3-arg find() to [clang-tidy] add support to 3-arg find() in StringFindStartswith.May 1 2020, 11:43 AM
niko edited the summary of this revision. (Show Details)

Correct me if I'm wrong, but that seems to be in violation of IWYU? Maybe I'm misreading this, or is the idea that higher-lever tooling (e.g. IWYU fixer) is supposed to address that?

It is but it sort of gets a pass as the absl/strings/match.h has to include it.
Similar to how if you #include <vector>, you wouldn't then #include <initializer_list> so you could construct a std::vector using a std::initializer_list.

Makes sense. Thanks!

niko updated this revision to Diff 288378.Aug 27 2020, 10:01 AM

Rebasing to HEAD

hokein accepted this revision.Aug 31 2020, 4:05 AM
This revision is now accepted and ready to land.Aug 31 2020, 4:05 AM