summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-09-14 13:59:33 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-09-14 14:43:43 +0200
commit5685ee25aad4a4debb47fb5896082be48f521d17 (patch)
treeba2d8f9341fc1124097e70cae64e1f07cc920712 /compilerplugins
parent44bfe8fad4f7c263dc713a65fb2ab0e2f9afcf99 (diff)
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 <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/redundantcast.cxx3
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx12
2 files changed, 15 insertions, 0 deletions
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<SubstTemplateTypeParmType>(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<typename T>
+struct EnumItemInterface {
+ T GetValue() { return static_cast<T>(0); }
+};
+class Enum1Item : public EnumItemInterface<Enum1> {
+};
+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: */