[clang-tools-extra] dcc2014 - [clang-tidy] UseDefaultMemberInitCheck::checkDefaultInit - Use cast<> instead of dyn_cast<> to avoid dereference of nullptr

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 12 07:36:08 PST 2022


Author: Simon Pilgrim
Date: 2022-01-12T15:35:37Z
New Revision: dcc20143e170b912aa0831dbedb5c1de07958012

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

LOG: [clang-tidy] UseDefaultMemberInitCheck::checkDefaultInit - Use cast<> instead of dyn_cast<> to avoid dereference of nullptr

The pointer is always dereferenced immediately below, so assert the cast is correct instead of returning nullptr

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
index d57a88d668832..6f8c984da2a8f 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -247,7 +247,7 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
 
   // Check whether we have multiple hand-written constructors and bomb out, as
   // it is hard to reconcile their sets of member initializers.
-  const auto *ClassDecl = dyn_cast<CXXRecordDecl>(Field->getParent());
+  const auto *ClassDecl = cast<CXXRecordDecl>(Field->getParent());
   if (llvm::count_if(ClassDecl->ctors(), [](const CXXConstructorDecl *Ctor) {
         return !Ctor->isCopyOrMoveConstructor();
       }) > 1)


        


More information about the cfe-commits mailing list