Skip to content

Commit 76d2f0f

Browse files
floventvbvictorEugeneZelenko
authored
[clang-tidy] readability-container-size-empty: Correctly generating fix hints when size method is called from implicit this (#152486)
Correctly generating fix hints when size method is called from implicit this. Closes #152387. --------- Co-authored-by: Baranov Victor <[email protected]> Co-authored-by: EugeneZelenko <[email protected]>
1 parent 09ff631 commit 76d2f0f

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,12 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
238238
? MemberCallObject
239239
: (Pointee ? Pointee : Result.Nodes.getNodeAs<Expr>("STLObject"));
240240
FixItHint Hint;
241-
std::string ReplacementText = std::string(
242-
Lexer::getSourceText(CharSourceRange::getTokenRange(E->getSourceRange()),
243-
*Result.SourceManager, getLangOpts()));
241+
std::string ReplacementText =
242+
E->isImplicitCXXThis()
243+
? ""
244+
: std::string(Lexer::getSourceText(
245+
CharSourceRange::getTokenRange(E->getSourceRange()),
246+
*Result.SourceManager, getLangOpts()));
244247
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E);
245248
if (isBinaryOrTernary(E) || isa<UnaryOperator>(E) ||
246249
(OpCallExpr && (OpCallExpr->getOperator() == OO_Star))) {
@@ -251,6 +254,8 @@ void ContainerSizeEmptyCheck::check(const MatchFinder::MatchResult &Result) {
251254
// This can happen if the object is a smart pointer. Don't add anything
252255
// because a '->' is already there (PR#51776), just call the method.
253256
ReplacementText += "empty()";
257+
} else if (E->isImplicitCXXThis()) {
258+
ReplacementText += "empty()";
254259
} else if (E->getType()->isPointerType())
255260
ReplacementText += "->empty()";
256261
else

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ Changes in existing checks
186186
<clang-tidy/checks/portability/template-virtual-member-function>` check to
187187
avoid false positives on pure virtual member functions.
188188

189+
- Improved :doc:`readability-container-size-empty
190+
<clang-tidy/checks/readability/container-size-empty>` check by correctly
191+
generating fix-it hints when size method is called from implicit ``this``.
192+
189193
- Improved :doc:`readability-identifier-naming
190194
<clang-tidy/checks/readability/identifier-naming>` check by ignoring
191195
declarations in system headers.

clang-tools-extra/test/clang-tidy/checkers/readability/container-size-empty.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,16 @@ namespace PR94454 {
895895
int operator""_ci() { return 0; }
896896
auto eq = 0_ci == 0;
897897
}
898+
899+
namespace GH152387 {
900+
901+
class foo : public std::string{
902+
void doit() {
903+
if (!size()) {
904+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: the 'empty' method should be used to check for emptiness instead of 'size'
905+
// CHECK-FIXES: if (empty()) {
906+
}
907+
}
908+
};
909+
910+
}

0 commit comments

Comments
 (0)