diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2017-09-26 08:35:32 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2017-09-26 10:01:54 +0200 |
commit | d883e3556a77303919c84652fea0f603fa350311 (patch) | |
tree | 0a71c279f555ec4727a510e8bd3307222886f913 /compilerplugins | |
parent | 2be37f4040567c0b09b703d31c231eea606a2715 (diff) |
loplugin:redundantcast: cstyle_cast within reinterpret_cast
Change-Id: Ie31c9dd6d8741aa856553b798bb5b7f695a3fe0f
Reviewed-on: https://gerrit.libreoffice.org/42776
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/redundantcast.cxx | 14 | ||||
-rw-r--r-- | compilerplugins/clang/test/redundantcast.cxx | 8 |
2 files changed, 22 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index dba573387743..30914a460dc4 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -522,6 +522,20 @@ bool RedundantCast::VisitCXXReinterpretCastExpr( expr->getExprLoc()) << expr->getSubExprAsWritten()->getType() << expr->getType() << expr->getSourceRange(); + } else if (expr->getType()->isFundamentalType()) { + if (auto const sub = dyn_cast<CXXConstCastExpr>( + expr->getSubExpr()->IgnoreParens())) + { + report( + DiagnosticsEngine::Warning, + ("redundant const_cast from %0 to %1 within reinterpret_cast to" + " fundamental type %2"), + expr->getExprLoc()) + << sub->getSubExprAsWritten()->getType() + << sub->getTypeAsWritten() << expr->getTypeAsWritten() + << expr->getSourceRange(); + return true; + } } return true; } diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 5a56ec42e32e..ff3e8392906a 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -7,6 +7,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <cstddef> + #include "redundantcast.hxx" void f1(char *) {} @@ -318,12 +320,18 @@ void testArithmeticTypedefs() { (void) (T1) nt1r(); // expected-error {{redundant}} } +void testReinterpretConstCast() { + int n = 0; + (void) reinterpret_cast<std::size_t>((const_cast<int const *>(&n))); // expected-error-re {{redundant const_cast from 'int *' to 'const int *' within reinterpret_cast to fundamental type 'std::size_t' (aka 'unsigned {{.+}}') [loplugin:redundantcast]}} +} + int main() { testConstCast(); testStaticCast(); testFunctionalCast(); testCStyleCast(); testCStyleCastOfTemplateMethodResult(nullptr); + testReinterpretConstCast(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |