[clang-tools-extra] 43e13fd - [NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (#71006)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 04:30:10 PDT 2023


Author: Congcong Cai
Date: 2023-11-02T19:30:06+08:00
New Revision: 43e13fdc9e8edd425f640c424071377879c07822

URL: https://github.com/llvm/llvm-project/commit/43e13fdc9e8edd425f640c424071377879c07822
DIFF: https://github.com/llvm/llvm-project/commit/43e13fdc9e8edd425f640c424071377879c07822.diff

LOG: [NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (#71006)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index b6daf8b936bde0f..23d7303371529da 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -118,45 +118,50 @@ static void updateAssignmentLevel(
   }
 }
 
-static std::pair<const FieldDecl *, const Expr *>
+struct AssignmentPair {
+  const FieldDecl *Field;
+  const Expr *Init;
+};
+
+static std::optional<AssignmentPair>
 isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
                        const CXXConstructorDecl *Ctor) {
   if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
     if (BO->getOpcode() != BO_Assign)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     const auto *ME = dyn_cast<MemberExpr>(BO->getLHS()->IgnoreParenImpCasts());
     if (!ME)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     const auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
     if (!Field)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     if (!isa<CXXThisExpr>(ME->getBase()))
-      return std::make_pair(nullptr, nullptr);
+      return {};
     const Expr *Init = BO->getRHS()->IgnoreParenImpCasts();
-    return std::make_pair(Field, Init);
+    return AssignmentPair{Field, Init};
   }
   if (const auto *COCE = dyn_cast<CXXOperatorCallExpr>(S)) {
     if (COCE->getOperator() != OO_Equal)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     const auto *ME =
         dyn_cast<MemberExpr>(COCE->getArg(0)->IgnoreParenImpCasts());
     if (!ME)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     const auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
     if (!Field)
-      return std::make_pair(nullptr, nullptr);
+      return {};
 
     if (!isa<CXXThisExpr>(ME->getBase()))
-      return std::make_pair(nullptr, nullptr);
+      return {};
     const Expr *Init = COCE->getArg(1)->IgnoreParenImpCasts();
-    return std::make_pair(Field, Init);
+    return AssignmentPair{Field, Init};
   }
-  return std::make_pair(nullptr, nullptr);
+  return {};
 }
 
 PreferMemberInitializerCheck::PreferMemberInitializerCheck(
@@ -216,11 +221,12 @@ void PreferMemberInitializerCheck::check(
         return;
     }
 
-    const FieldDecl *Field = nullptr;
-    const Expr *InitValue = nullptr;
-    std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S, Ctor);
-    if (!Field)
+    std::optional<AssignmentPair> AssignmentToMember =
+        isAssignmentToMemberOf(Class, S, Ctor);
+    if (!AssignmentToMember)
       continue;
+    const FieldDecl *Field = AssignmentToMember->Field;
+    const Expr *InitValue = AssignmentToMember->Init;
     updateAssignmentLevel(Field, InitValue, Ctor, AssignedFields);
     if (!canAdvanceAssignment(AssignedFields[Field]))
       continue;


        


More information about the cfe-commits mailing list