From 5685ee25aad4a4debb47fb5896082be48f521d17 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 14 Sep 2017 13:59:33 +0200 Subject: improve redundantcast loplugin to find c-style casts where the expression is a templated method Change-Id: Ifbd1e2cdc72d906fc95a7ec0f9408c3f6d2a836b Reviewed-on: https://gerrit.libreoffice.org/42275 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/redundantcast.cxx | 3 +++ compilerplugins/clang/test/redundantcast.cxx | 12 ++++++++++++ 2 files changed, 15 insertions(+) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 19393e0c6449..32f7e8bd7fd9 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -294,6 +294,9 @@ bool RedundantCast::VisitCStyleCastExpr(CStyleCastExpr const * expr) { } auto t1 = compat::getSubExprAsWritten(expr)->getType(); auto t2 = expr->getTypeAsWritten(); + if (auto templateType = dyn_cast(t1)) { + t1 = templateType->desugar(); + } if (t1 != t2) { return true; } diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 9eda22a04b0f..db1268b8b87b 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -290,11 +290,23 @@ void testCStyleCast() { (void)e; } +template +struct EnumItemInterface { + T GetValue() { return static_cast(0); } +}; +class Enum1Item : public EnumItemInterface { +}; +bool testCStyleCastOfTemplateMethodResult(Enum1Item* item) { + return (Enum1)item->GetValue() == Enum1::X; // expected-error {{redundant cstyle cast from 'Enum1' to 'Enum1' [loplugin:redundantcast]}} +} + + int main() { testConstCast(); testStaticCast(); testFunctionalCast(); testCStyleCast(); + testCStyleCastOfTemplateMethodResult(nullptr); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit