This is an archive of the discontinued LLVM Phabricator instance.

Give clang-tidy readability-redundant-string-init a customizable list of string types to fix
ClosedPublic

Authored by poelmanc on Oct 28 2019, 10:27 PM.
Tokens
"Like" token, awarded by MyDeveloperDay.

Details

Summary

This patch adds a feature requested by @MyDeveloperDay at https://reviews.llvm.org/D69238 to enable readability-redundant-string-init to take a list of strings to apply the fix to rather than hard-coding basic_string. It adds a StringNames option of semicolon-delimited names of string classes to which to apply this fix. Tests ensure this works with test class out::TestString as well as std::string and std::wstring as before. It should be applicable to llvm::StringRef, QString, etc.

Diff Detail

Event Timeline

poelmanc created this revision.Oct 28 2019, 10:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 28 2019, 10:27 PM

Please mention new option in Release Notes (in new options section, if there is no such yet, see previous release for order of sections).

clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
15

Please remove empty line.

clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
27

Please use single back-ticks for options. Same below.

Eugene.Zelenko retitled this revision from Give clang-tidy readability-redundant-string-init a customizable list of string types to fix to [clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix.Oct 29 2019, 5:18 AM
Eugene.Zelenko removed a subscriber: MyDeveloperDay.
MyDeveloperDay retitled this revision from [clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix to Give clang-tidy readability-redundant-string-init a customizable list of string types to fix.Oct 29 2019, 5:19 AM
MyDeveloperDay added a reviewer: JonasToth.

Thanks you so much for this patch, I obviously like it! So many of us who write our own string classes we tend to make them have similar interfaces to std::string anyway for obvious reasons (some of us actually add decent startWith,endsWith and contains functions!), so I think this is a great addition... I'm gonna mention that there are other checks that are pertinent, but let us see if this one gets accepted first. You get my LGTM, but let's run it by the code owners.

aaron.ballman added inline comments.Oct 29 2019, 1:44 PM
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
21

I think the default should probably be ::std::basic_string to avoid getting other things named basic_string?

clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
27

Should update this as well if you go with the suggest change to ::std::basic_string.

clang/include/clang/ASTMatchers/ASTMatchers.h
2563–2565 ↗(On Diff #226836)

I think we should try to get the existing hasAnyName() matcher to work with a vector of strings instead of adding a new matcher to do the same thing.

poelmanc updated this revision to Diff 226987.Oct 29 2019, 3:56 PM

Added release notes, fixed backticks in documentation, removed blank line, removed new hasListedName matcher and used existing hasAnyName matcher.

poelmanc marked 3 inline comments as done.Oct 29 2019, 4:06 PM

@aaron.ballman Thanks for the hasAnyName feedback! From the name internal::VariadicFunction I assumed arguments were needed at compile-time, but thanks to your suggestion I found the overload taking ArrayRef<ArgT>.

poelmanc marked an inline comment as done.Oct 29 2019, 4:15 PM
poelmanc added inline comments.
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
21

The prior code had basic_string hard-coded at lines 27, 31, and 50 so I initially set the default to basic_string just to keep the default behavior unchanged.

I just now tried setting it to each of std::basic_string, ::std::basic_string, and even std::string;std::wstring and the readability-redundant-string-init.cpp tests failed with any of those settings, so I've left it set to basic_string.

gribozavr2 requested changes to this revision.Oct 31 2019, 10:29 AM
gribozavr2 added a subscriber: gribozavr2.
gribozavr2 added inline comments.
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
21

I think we should change the checker behavior so that it checks against the fully-qualified name.

We are defining a user-settable option, and changing its behavior later to allow fully-qualified names could be difficult.

The reason why tests failed for you is because below the name "basic_string" is used as a method name (the name of the constructor). That checker should be rewritten to check the name of the type.

This revision now requires changes to proceed.Oct 31 2019, 10:29 AM
poelmanc updated this revision to Diff 228541.Nov 8 2019, 4:33 PM
poelmanc edited the summary of this revision. (Show Details)

Now allows namespaces on types and defaults to ::std::basic_string as requested. The code uses namespaced string type names to check types, and uses non-namespaced string type names to check for the required one-argument or two-argument-defaulted constructors.

poelmanc marked 2 inline comments as done.Nov 8 2019, 4:34 PM
mgehre removed a subscriber: mgehre.Nov 9 2019, 12:25 AM
gribozavr2 accepted this revision.Nov 11 2019, 8:55 AM
gribozavr2 added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
29

"of class names"

I don't think there's anything about base classes here.

30

Please wrap std::string and std::wstring in backtics.

This revision is now accepted and ready to land.Nov 11 2019, 8:55 AM
Eugene.Zelenko added inline comments.Nov 11 2019, 9:03 AM
clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
30

Double back-ticks, because names are language constructs here.

poelmanc updated this revision to Diff 228769.Nov 11 2019, 1:56 PM

Make requested fixes to documentation.

poelmanc marked 3 inline comments as done.Nov 11 2019, 1:57 PM
This revision was automatically updated to reflect the committed changes.