[Libreoffice-commits] core.git: compilerplugins/clang
Stephan Bergmann
sbergman at redhat.com
Thu Feb 16 16:43:56 UTC 2017
compilerplugins/clang/redundantcast.cxx | 13 ++++++++-----
compilerplugins/clang/test/redundantcast.cxx | 4 ++--
2 files changed, 10 insertions(+), 7 deletions(-)
New commits:
commit 440720e14deb7020ba8d3158c3325c2d588664a1
Author: Stephan Bergmann <sbergman at redhat.com>
Date: Thu Feb 16 17:42:53 2017 +0100
loplugin:redundantcast: Avoid double warnings on some const_cast
Change-Id: I1e6140fef55054899dd32465726e804fc6006394
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index 6d18f2a..c7561a7 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 7af9716..e8be301 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]}}
More information about the Libreoffice-commits
mailing list