diff options
-rw-r--r-- | compilerplugins/clang/redundantcast.cxx | 13 | ||||
-rw-r--r-- | compilerplugins/clang/test/redundantcast.cxx | 4 |
2 files changed, 10 insertions, 7 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 6d18f2abffd8..c7561a7a6bae 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -107,6 +107,12 @@ bool isVoidPointer(QualType type) { && type->getAs<clang::PointerType>()->getPointeeType()->isVoidType(); } +bool isRedundantConstCast(CXXConstCastExpr const * expr) { + return expr->getTypeAsWritten().getCanonicalType().getTypePtr() + == (expr->getSubExprAsWritten()->getType().getCanonicalType() + .getTypePtr()); +} + class RedundantCast: public RecursiveASTVisitor<RedundantCast>, public loplugin::RewritePlugin { @@ -168,7 +174,7 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { { auto e = dyn_cast<CXXConstCastExpr>( expr->getSubExpr()->IgnoreParenImpCasts()); - if (e != nullptr) { + if (e != nullptr && !isRedundantConstCast(e)) { auto t1 = e->getSubExpr()->getType().getCanonicalType(); auto t2 = expr->getType().getCanonicalType(); bool ObjCLifetimeConversion; @@ -392,10 +398,7 @@ bool RedundantCast::VisitCXXConstCastExpr(CXXConstCastExpr const * expr) { if (ignoreLocation(expr)) { return true; } - if (expr->getTypeAsWritten().getCanonicalType().getTypePtr() - == (expr->getSubExprAsWritten()->getType().getCanonicalType() - .getTypePtr())) - { + if (isRedundantConstCast(expr)) { report( DiagnosticsEngine::Warning, "redundant const_cast from %0 to %1", expr->getExprLoc()) diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 7af9716fa27c..e8be3013cd86 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -19,8 +19,8 @@ int main() { f1(const_cast<char * const>(p1)); // expected-error {{redundant const_cast from 'char *' to 'char *const' [loplugin:redundantcast]}} f1(const_cast<char *>(p2)); f1(const_cast<char * const>(p2)); - f2(const_cast<char *>(p1)); // expected-error {{redundant const_cast from 'char *' to 'char *', result is implictly cast to 'const char *' [loplugin:redundantcast]}} expected-error {{redundant const_cast from 'char *' to 'char *' [loplugin:redundantcast]}} - f2(const_cast<char * const>(p1)); // expected-error {{redundant const_cast from 'char *' to 'char *', result is implictly cast to 'const char *' [loplugin:redundantcast]}} expected-error {{redundant const_cast from 'char *' to 'char *const' [loplugin:redundantcast]}} + f2(const_cast<char *>(p1)); // expected-error {{redundant const_cast from 'char *' to 'char *' [loplugin:redundantcast]}} + f2(const_cast<char * const>(p1)); // expected-error {{redundant const_cast from 'char *' to 'char *const' [loplugin:redundantcast]}} f2(const_cast<char const *>(p1)); f2(const_cast<char const * const>(p1)); f2(const_cast<char *>(p2)); // expected-error {{redundant const_cast from 'const char *' to 'char *', result is implictly cast to 'const char *' [loplugin:redundantcast]}} |