@@ -19,15 +19,12 @@ using namespace clang::ast_matchers;
1919namespace clang ::tidy::generalsgamecode::readability {
2020
2121void UseIsEmptyCheck::registerMatchers (MatchFinder *Finder) {
22- // Match member calls to getLength() on AsciiString or UnicodeString
23- // followed by comparison with 0
2422 auto GetLengthCall = cxxMemberCallExpr (
2523 callee (cxxMethodDecl (hasName (" getLength" ))),
2624 on (hasType (hasUnqualifiedDesugaredType (
2725 recordType (hasDeclaration (cxxRecordDecl (
2826 hasAnyName (" AsciiString" , " UnicodeString" ))))))));
2927
30- // Match: obj.getLength() == 0
3128 Finder->addMatcher (
3229 binaryOperator (
3330 hasOperatorName (" ==" ),
@@ -36,7 +33,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
3633 .bind (" comparison" ),
3734 this );
3835
39- // Match: obj.getLength() != 0
4036 Finder->addMatcher (
4137 binaryOperator (
4238 hasOperatorName (" !=" ),
@@ -45,7 +41,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
4541 .bind (" comparison" ),
4642 this );
4743
48- // Match: obj.getLength() > 0
4944 Finder->addMatcher (
5045 binaryOperator (
5146 hasOperatorName (" >" ),
@@ -54,7 +49,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
5449 .bind (" comparison" ),
5550 this );
5651
57- // Match: obj.getLength() <= 0
5852 Finder->addMatcher (
5953 binaryOperator (
6054 hasOperatorName (" <=" ),
@@ -63,7 +57,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
6357 .bind (" comparison" ),
6458 this );
6559
66- // Match: 0 == obj.getLength()
6760 Finder->addMatcher (
6861 binaryOperator (
6962 hasOperatorName (" ==" ),
@@ -72,7 +65,6 @@ void UseIsEmptyCheck::registerMatchers(MatchFinder *Finder) {
7265 .bind (" comparison" ),
7366 this );
7467
75- // Match: 0 != obj.getLength()
7668 Finder->addMatcher (
7769 binaryOperator (
7870 hasOperatorName (" !=" ),
@@ -90,12 +82,10 @@ void UseIsEmptyCheck::check(const MatchFinder::MatchResult &Result) {
9082 if (!Comparison || !GetLengthCall)
9183 return ;
9284
93- // Get the object on which getLength() is called
9485 const Expr *ObjectExpr = GetLengthCall->getImplicitObjectArgument ();
9586 if (!ObjectExpr)
9687 return ;
9788
98- // Determine the replacement based on the operator
9989 StringRef Operator = Comparison->getOpcodeStr ();
10090 bool ShouldNegate = false ;
10191
@@ -108,23 +98,20 @@ void UseIsEmptyCheck::check(const MatchFinder::MatchResult &Result) {
10898 } else if (Operator == " <=" ) {
10999 ShouldNegate = false ;
110100 } else {
111- return ; // Unsupported operator
101+ return ;
112102 }
113103
114- // Get the source text for the object expression
115104 StringRef ObjectText = Lexer::getSourceText (
116105 CharSourceRange::getTokenRange (ObjectExpr->getSourceRange ()),
117106 *Result.SourceManager , Result.Context ->getLangOpts ());
118107
119- // Build the replacement text
120108 std::string Replacement;
121109 if (ShouldNegate) {
122110 Replacement = " !" + ObjectText.str () + " .isEmpty()" ;
123111 } else {
124112 Replacement = ObjectText.str () + " .isEmpty()" ;
125113 }
126114
127- // Create the fix - replace the entire comparison
128115 SourceLocation StartLoc = Comparison->getBeginLoc ();
129116 SourceLocation EndLoc = Comparison->getEndLoc ();
130117
0 commit comments