summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-02-16 17:42:53 +0100
committerStephan Bergmann <sbergman@redhat.com>2017-02-16 17:43:23 +0100
commit440720e14deb7020ba8d3158c3325c2d588664a1 (patch)
tree6f1b3896e2494f34deca4cae74ac40335b1a763a
parent6d3db33328fb2fec3539c828d062cad6d16d3195 (diff)
loplugin:redundantcast: Avoid double warnings on some const_cast
Change-Id: I1e6140fef55054899dd32465726e804fc6006394
-rw-r--r--compilerplugins/clang/redundantcast.cxx13
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx4
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]}}