Skip to content

Conversation

@HerrCai0907
Copy link
Contributor

combine type matcher and optional type check.

…sion

combine type matcher and optional type check.
@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

combine type matcher and optional type check.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp (+8-9)
diff --git a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
index 600eab37552766..f2ff27d85fb004 100644
--- a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
@@ -43,18 +43,20 @@ OptionalValueConversionCheck::getCheckTraversalKind() const {
 }
 
 void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
-  auto ConstructTypeMatcher =
-      qualType(hasCleanType(qualType().bind("optional-type")));
+  auto BindOptionalType = qualType(
+      hasCleanType(qualType(hasDeclaration(namedDecl(
+                                matchers::matchesAnyListedName(OptionalTypes))))
+                       .bind("optional-type")));
 
-  auto CallTypeMatcher =
+  auto EqualsBoundOptionalType =
       qualType(hasCleanType(equalsBoundNode("optional-type")));
 
   auto OptionalDereferenceMatcher = callExpr(
       anyOf(
           cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
-                              hasUnaryOperand(hasType(CallTypeMatcher)))
+                              hasUnaryOperand(hasType(EqualsBoundOptionalType)))
               .bind("op-call"),
-          cxxMemberCallExpr(thisPointerType(CallTypeMatcher),
+          cxxMemberCallExpr(thisPointerType(EqualsBoundOptionalType),
                             callee(cxxMethodDecl(anyOf(
                                 hasOverloadedOperatorName("*"),
                                 matchers::matchesAnyListedName(ValueMethods)))))
@@ -66,10 +68,7 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
                hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
   Finder->addMatcher(
       cxxConstructExpr(
-          argumentCountIs(1U),
-          hasDeclaration(cxxConstructorDecl(
-              ofClass(matchers::matchesAnyListedName(OptionalTypes)))),
-          hasType(ConstructTypeMatcher),
+          argumentCountIs(1U), hasType(BindOptionalType),
           hasArgument(0U, ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
                                                  StdMoveCallMatcher))),
           unless(anyOf(hasAncestor(typeLoc()),

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

LGTM

@HerrCai0907 HerrCai0907 merged commit e69d8b8 into llvm:main Dec 10, 2024
11 checks passed
@HerrCai0907 HerrCai0907 deleted the refactor-OptionalValueConversionCheck branch December 10, 2024 10:00
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.

3 participants