[PATCH] D13368: [clang-tidy] add check cppcoreguidelines-pro-type-static-cast-downcast
Samuel Benzaquen via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 2 08:15:37 PDT 2015
sbenza added inline comments.
================
Comment at: test/clang-tidy/cppcoreguidelines-pro-type-static-cast-downcast.cpp:19
@@ +18,3 @@
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use static_cast to cast from base to derived. Use dynamic_cast instead. (C++ Core Guidelines, rule Type.2) [cppcoreguidelines-pro-type-static-cast-downcast]
+ // CHECK-FIXES: auto B0 = dynamic_cast<Derived*>(new Base());
+
----------------
This is not a valid dynamic_cast<> because Base is not polymorphic.
================
Comment at: test/clang-tidy/cppcoreguidelines-pro-type-static-cast-downcast.cpp:47
@@ +46,3 @@
+ auto B0 = static_cast<B*>(new D());
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use static_cast to cast from base to derived. Use dynamic_cast instead. (C++ Core Guidelines, rule Type.2) [cppcoreguidelines-pro-type-static-cast-downcast]
+ // CHECK-FIXES: auto B0 = dynamic_cast<B*>(new D());
----------------
I don't think we should warn here.
Whether it is a downcast or whether dynamic_cast<> is correct will depend on the specific instantiation.
A fix like this can break valid code.
http://reviews.llvm.org/D13368
More information about the cfe-commits
mailing list