This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] readability-container-size-empty - detect missing usage of .empty() on string_literals
ClosedPublic

Authored by felix642 on Aug 19 2023, 12:13 PM.
Tokens
"Like" token, awarded by PiotrZSL.

Details

Summary

Detect comparison between string and empty string_literals.

Fixes #64547

Diff Detail

Event Timeline

felix642 created this revision.Aug 19 2023, 12:13 PM
felix642 requested review of this revision.Aug 19 2023, 12:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2023, 12:13 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
PiotrZSL requested changes to this revision.Aug 19 2023, 12:30 PM
  • Release notes entry is missing.
  • Commit/Change description should be updated
clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
171

Probably best would be to write matcher like this:

AST_MATCHER_P(UserDefinedLiteral, hasLiteral, Matcher<Expr>, InnerMatcher) {
   if (const Expr* CookedLiteral  = Node.getCookedLiteral()) {
      return InnerMatcher.matches(CookedLiteral, Finder, Builder);
   }
   return false;
}

and use it instead of hasDescendant

userDefinedLiteral(hasLiteral(stringLiteral(hasSize(0)))),
clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
791

Add this new line

This revision now requires changes to proceed.Aug 19 2023, 12:30 PM
felix642 updated this revision to Diff 551785.Aug 19 2023, 1:31 PM

Fixed tests and addressed comments.

felix642 marked 2 inline comments as done.Aug 19 2023, 1:33 PM

Hi @PiotrZSL, thank you for the feedback.

I have addressed most of your comments, but I'm not sure to understand what you mean by "Commit/Change description should be updated". Would you be able to clarify that for me?

PiotrZSL accepted this revision.Aug 19 2023, 2:29 PM

LGTM, run clang-format on this code....

This revision is now accepted and ready to land.Aug 19 2023, 2:29 PM
felix642 updated this revision to Diff 551789.Aug 19 2023, 2:34 PM

Clang-format

PiotrZSL retitled this revision from [clang-tidy] [readability-container-size-empty] improved check to detect missing usage of .empty() on string_literals Fixes #64547 to [clang-tidy] readability-container-size-empty - detect missing usage of .empty() on string_literals .Aug 21 2023, 10:42 AM
PiotrZSL edited the summary of this revision. (Show Details)
rnk added a subscriber: rnk.Aug 22 2023, 6:43 PM
rnk added inline comments.
clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp
26

I discovered that this test started to fail when I landed my revert (0d9919d362a7a70b2a7970861d897ecc47ec9e4d) of f2583f3acf596cc545c8c0e3cb28e712f4ebf21b. I "fixed" the test by changing this to operator""_s in ba52a10fca6fc7b791894c584233db012def68a5, but I'm not sure if that changes the meaning of the test. Please review that or follow up on this.