Skip to content

[clang-tidy] NFCI: remove non-functional matcher from SizeofExpressionCheck #142654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mizvekov
Copy link
Contributor

@mizvekov mizvekov commented Jun 3, 2025

This matcher would never match anything, because all record types as-written would be wrapped in an ElaboratedType.

Just fixing that leads to a matcher which has too many false positives to be useful.

The warning message itself is not great either, it has a hard-coded type name.

…nCheck

This matcher would never match anything, because all record types as-written
would be wrappen in an ElaboratedType.

Just fixing that leads to a matcher which has too many false positives
to be useful.

The warning message itself is not great either, it has a hard-coded type
name.
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-clang-tidy

Author: Matheus Izvekov (mizvekov)

Changes

This matcher would never match anything, because all record types as-written would be wrappen in an ElaboratedType.

Just fixing that leads to a matcher which has too many false positives to be useful.

The warning message itself is not great either, it has a hard-coded type name.


Full diff: https://github.com/llvm/llvm-project/pull/142654.diff

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+7-18)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index f3d4c2255d86e..9eeba867f5211 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -170,8 +170,6 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
 
     const auto PointerToStructType =
         hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
-    const auto PointerToStructTypeWithBinding =
-        type(PointerToStructType).bind("struct-type");
     const auto PointerToStructExpr =
         expr(hasType(hasCanonicalType(PointerToStructType)));
 
@@ -188,12 +186,10 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
         ignoringParenImpCasts(unaryOperator(hasOperatorName("*")));
 
     Finder->addMatcher(
-        expr(sizeOfExpr(anyOf(has(ignoringParenImpCasts(
-                                  expr(PointerToDetectedExpr, unless(DerefExpr),
-                                       unless(SubscriptExprWithZeroIndex),
-                                       unless(VarWithConstStrLiteralDecl),
-                                       unless(cxxThisExpr())))),
-                              has(PointerToStructTypeWithBinding))))
+        expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
+                 PointerToDetectedExpr, unless(DerefExpr),
+                 unless(SubscriptExprWithZeroIndex),
+                 unless(VarWithConstStrLiteralDecl), unless(cxxThisExpr()))))))
             .bind("sizeof-pointer"),
         this);
   }
@@ -354,16 +350,9 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
          "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
         << E->getSourceRange();
   } else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
-    if (Result.Nodes.getNodeAs<Type>("struct-type")) {
-      diag(E->getBeginLoc(),
-           "suspicious usage of 'sizeof(A*)' on pointer-to-aggregate type; did "
-           "you mean 'sizeof(A)'?")
-          << E->getSourceRange();
-    } else {
-      diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
-                             "of pointer type")
-          << E->getSourceRange();
-    }
+    diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
+                           "of pointer type")
+        << E->getSourceRange();
   } else if (const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
                  "sizeof-compare-constant")) {
     diag(E->getOperatorLoc(),

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Matheus Izvekov (mizvekov)

Changes

This matcher would never match anything, because all record types as-written would be wrappen in an ElaboratedType.

Just fixing that leads to a matcher which has too many false positives to be useful.

The warning message itself is not great either, it has a hard-coded type name.


Full diff: https://github.com/llvm/llvm-project/pull/142654.diff

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+7-18)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index f3d4c2255d86e..9eeba867f5211 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -170,8 +170,6 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
 
     const auto PointerToStructType =
         hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
-    const auto PointerToStructTypeWithBinding =
-        type(PointerToStructType).bind("struct-type");
     const auto PointerToStructExpr =
         expr(hasType(hasCanonicalType(PointerToStructType)));
 
@@ -188,12 +186,10 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
         ignoringParenImpCasts(unaryOperator(hasOperatorName("*")));
 
     Finder->addMatcher(
-        expr(sizeOfExpr(anyOf(has(ignoringParenImpCasts(
-                                  expr(PointerToDetectedExpr, unless(DerefExpr),
-                                       unless(SubscriptExprWithZeroIndex),
-                                       unless(VarWithConstStrLiteralDecl),
-                                       unless(cxxThisExpr())))),
-                              has(PointerToStructTypeWithBinding))))
+        expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
+                 PointerToDetectedExpr, unless(DerefExpr),
+                 unless(SubscriptExprWithZeroIndex),
+                 unless(VarWithConstStrLiteralDecl), unless(cxxThisExpr()))))))
             .bind("sizeof-pointer"),
         this);
   }
@@ -354,16 +350,9 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
          "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
         << E->getSourceRange();
   } else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
-    if (Result.Nodes.getNodeAs<Type>("struct-type")) {
-      diag(E->getBeginLoc(),
-           "suspicious usage of 'sizeof(A*)' on pointer-to-aggregate type; did "
-           "you mean 'sizeof(A)'?")
-          << E->getSourceRange();
-    } else {
-      diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
-                             "of pointer type")
-          << E->getSourceRange();
-    }
+    diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
+                           "of pointer type")
+        << E->getSourceRange();
   } else if (const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
                  "sizeof-compare-constant")) {
     diag(E->getOperatorLoc(),

@jansvoboda11 jansvoboda11 removed their request for review June 3, 2025 19:31
Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
maybe wait for the other reviewer

@HerrCai0907 HerrCai0907 requested review from PiotrZSL and 5chmidti June 4, 2025 01:44
Copy link
Contributor

@carlosgalvezp carlosgalvezp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, if all tests pass then this must be dead code! Thanks for cleaning up.

@carlosgalvezp carlosgalvezp merged commit c95189f into main Jun 4, 2025
14 checks passed
@carlosgalvezp carlosgalvezp deleted the users/mizvekov/SizeofExpressionCheck-remove-nonfunctional-matcher branch June 4, 2025 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants