diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2018-05-16 10:16:01 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2018-05-17 08:22:14 +0200 |
commit | 7ab34b51f2d45137191145d31b4b0c7d18f577bf (patch) | |
tree | abb760e86740a0e073008047d966af250e8c8c5d /compilerplugins | |
parent | f8e3ad02737854686a590f7f8f02eb72e03a0f8e (diff) |
loplugin:redundantcast improvements for floating-integer conversions
Change-Id: I63dbf18f144a792ae775fe6706da81657f790016
Reviewed-on: https://gerrit.libreoffice.org/54416
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 | 17 | ||||
-rw-r--r-- | compilerplugins/clang/test/redundantcast.cxx | 10 |
2 files changed, 27 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 0faadc5a541a..66b81941e579 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -248,6 +248,23 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { } } break; + case CK_FloatingToIntegral: + case CK_IntegralToFloating: + if (auto e = dyn_cast<ExplicitCastExpr>(expr->getSubExpr()->IgnoreParenImpCasts())) { + if ((isa<CXXStaticCastExpr>(e) || isa<CXXFunctionalCastExpr>(e)) + && (e->getSubExprAsWritten()->getType().getCanonicalType().getTypePtr() + == expr->getType().getCanonicalType().getTypePtr())) + { + report( + DiagnosticsEngine::Warning, + ("suspicious %select{static_cast|functional cast}0 from %1 to %2, result is" + " implicitly cast to %3"), + e->getExprLoc()) + << isa<CXXFunctionalCastExpr>(e) << e->getSubExprAsWritten()->getType() + << e->getTypeAsWritten() << expr->getType() << expr->getSourceRange(); + } + } + break; default: break; } diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 70fcdf3340cb..713a3be1433c 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -374,6 +374,15 @@ void testOverloadResolution() { (void) NonOverloadMemFn(&Overload::nonOverload); // expected-error {{redundant functional cast from 'void (Overload::*)()' to 'NonOverloadMemFn' (aka 'void (Overload::*)()') [loplugin:redundantcast]}} }; +void testIntermediaryStaticCast() { + int n = 0; + n = static_cast<double>(n); // expected-error {{suspicious static_cast from 'int' to 'double', result is implicitly cast to 'int' [loplugin:redundantcast]}} + n = double(n); // expected-error {{suspicious functional cast from 'int' to 'double', result is implicitly cast to 'int' [loplugin:redundantcast]}} + double d = 0.0; + d = static_cast<int>(d) + 1.0; // expected-error {{suspicious static_cast from 'double' to 'int', result is implicitly cast to 'double' [loplugin:redundantcast]}} + d = int(d) + 1.0; // expected-error {{suspicious functional cast from 'double' to 'int', result is implicitly cast to 'double' [loplugin:redundantcast]}} +}; + int main() { testConstCast(); testStaticCast(); @@ -382,6 +391,7 @@ int main() { testCStyleCastOfTemplateMethodResult(nullptr); testReinterpretConstCast(); testDynamicCast(); + testIntermediaryStaticCast(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |