This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] abseil-str-cat-append
ClosedPublic

Authored by hugoeg on Aug 21 2018, 1:58 PM.

Details

Summary

Flags uses of absl::StrCat to append to a string. Suggests absl::StrAppend should be used instead.

The check was developed internally and has been running at google, this is just
a move to open source the check. It was originally written by @bkramer and has been modified for external use.

Diff Detail

Event Timeline

hugoeg created this revision.Aug 21 2018, 1:58 PM
Eugene.Zelenko added inline comments.
clang-tidy/abseil/StrCatAppendCheck.cpp
28

while (true) will be better

51

Please run Clang-format.

docs/ReleaseNotes.rst
60

Please add new check entry in alphabetical order.

63

Please enclose absl::StrCat in `` and add (). same for absl::StrAppend. What does string? std::string?

docs/clang-tidy/checks/abseil-str-cat-append.rst
7

Please synchronize with Release Notes.

Eugene.Zelenko set the repository for this revision to rCTE Clang Tools Extra.
Eugene.Zelenko added a subscriber: cfe-commits.

Please make sure the code follows the LLVM code style, e.g. the variable name format "CamelName".

JonasToth added inline comments.
clang-tidy/abseil/StrCatAppendCheck.cpp
29

You can ellide the braces for the single stmt ifs

53

The naming is ambigous. str_cat can easily be mixed with strcat. Is this constant even necessary or could you use the literal in the functionDecl matcher?

86

Please make this comment a full sentence

88

I think it would be better to include absl::StrCat in the diagnostic, as it is more clear.
Same opinion on the other diag.

test/clang-tidy/abseil-str-cat-append.cpp
92

What happens if StrCat is used e.g. in an std::accumulate call as the binary operator? (https://en.cppreference.com/w/cpp/algorithm/accumulate the Example includes such a case)
Is this diagnosed, too?

The general case would be if StrCat is used as a template argument for a functor.

115

Could you please add a testcase that is outside of the absl namespace.

void f() {
  std::string s;
  s = absl::StrCat(s,s);
}

and

void g() {
  std::string s;
  using absl::StrCat;
  s = StrCat(s, s);
}

with better naming.

hugoeg updated this revision to Diff 161970.Aug 22 2018, 8:34 AM
hugoeg marked 11 inline comments as done.

applied corrections from comments

hugoeg added inline comments.Aug 22 2018, 8:35 AM
test/clang-tidy/abseil-str-cat-append.cpp
92

it doesn't diagnose those cases. and diagnosing it doesn't seem to be worthwhile

hugoeg updated this revision to Diff 161974.Aug 22 2018, 8:56 AM

minor fixes

hugoeg marked an inline comment as done.Aug 22 2018, 8:56 AM
JonasToth added inline comments.Aug 22 2018, 9:04 AM
test/clang-tidy/abseil-str-cat-append.cpp
92

You could add it as a known limitation to the docs / or as TODO in the code.

hugoeg updated this revision to Diff 161982.Aug 22 2018, 9:56 AM
hugoeg marked an inline comment as done.
docs/ReleaseNotes.rst
70

Please enclose std::string into ``

72

Details belongs to documentation.

hugoeg updated this revision to Diff 161995.Aug 22 2018, 10:36 AM
hugoeg marked 2 inline comments as done.

Let me know if there's anything else I can fix to move the process along.

docs/clang-tidy/checks/abseil-str-cat-append.rst
12

Please add namespace.

hugoeg updated this revision to Diff 162231.Aug 23 2018, 10:43 AM
hugoeg marked an inline comment as done.
hugoeg updated this revision to Diff 162253.Aug 23 2018, 12:53 PM

minor fixes, style improvements

hugoeg edited the summary of this revision. (Show Details)Aug 23 2018, 1:36 PM
hugoeg edited the summary of this revision. (Show Details)
JonasToth added inline comments.Aug 24 2018, 1:18 AM
clang-tidy/abseil/StrCatAppendCheck.cpp
22

I think you can elide the empty line around the matcher.

50

Not sure if this comment adds value and might even be a little misleading. StrCat itself does not look for calls, it is a helper to do so.
Maybe you can move the comment to the actual matching part with everything composed and explain more what is specifically looked for.

52

Please make this comment a sentence. Match that the arguments ... would be ok i think.

58

Please rename this variable to follow the convention

80

please add an assert(Op != nullptr && Call != nullptr && "Matcher does not work as expected"), even though it seems trivial its better then nullptr deref.
And the logic might change in the future, so better be save

81

I think // Handles the case 'x = absl::StrCat(x)', which does nothing. would be better

83

please use 'absl::StrCat' in the diagnostics (same below) to signify that it is code.

aaron.ballman added inline comments.Aug 24 2018, 8:13 AM
clang-tidy/abseil/StrCatAppendCheck.cpp
83

"does nothing" is not quite correct either; it does something, just not what the user expects. How about call to 'absl::StrCat' has no effect?

92–93

This wins the award for "kindest diagnostic message". :-D How about: call 'absl::StrAppend' instead of 'absl::StrCat' when appending to a string?

That said, the diagnostic doesn't say *why* the code is wrong in the first place. The documentation doesn't really go into it either. Is it a correctness issue, a performance issue, something else?

hugoeg updated this revision to Diff 162390.Aug 24 2018, 9:05 AM
hugoeg marked 9 inline comments as done.

applied corrections from comments

hugoeg updated this revision to Diff 162395.Aug 24 2018, 9:27 AM

ran svn update

aaron.ballman added inline comments.Aug 24 2018, 9:48 AM
clang-tidy/abseil/StrCatAppendCheck.cpp
92–93

So this is a performance concern, not a correctness concern? In that case, how about: call 'absl::StrAppend' instead of 'absl::StrCat' to avoid a performance penalty when appending to a string?

hugoeg updated this revision to Diff 162403.Aug 24 2018, 9:57 AM
hugoeg marked an inline comment as done.
This revision is now accepted and ready to land.Aug 24 2018, 10:24 AM

@aaron.ballman when you get the chance could you take another look at this and commit if it is ready? My internship ends rather soon and this is a tad time sensitive and I don't have commit access. Thank you for your time!

@aaron.ballman when you get the chance could you take another look at this and commit if it is ready? My internship ends rather soon and this is a tad time sensitive and I don't have commit access. Thank you for your time!

I've commit in r340915. Thank you for the patch!

hokein closed this revision.Aug 31 2018, 1:22 AM

Thanks @aaron.ballman. I'm closing it now as it is committed in rL340915.