From 0763d8413ed87d17fe2510608e2d7399834a5388 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Fri, 2 Jun 2017 15:22:39 +0200 Subject: Reduce loplugin:redundantcast warnings about functional casts even futher Change-Id: I8884e17c453831e048c43012ee176093c5b2f99e --- compilerplugins/clang/redundantcast.cxx | 9 +++++---- compilerplugins/clang/test/redundantcast.cxx | 8 +++++++- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 87bed77a22f8..4e79415c4850 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -539,11 +539,12 @@ bool RedundantCast::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * exp // // std::initializer_list{bar, baz} // - // ), and only to cases where the sub-expression already is a prvalue (and - // thus the cast is unlikely meant to create a temporary): + // ), and only to cases where the sub-expression already is a prvalue of + // non-class type (and thus the cast is unlikely meant to create a + // temporary): auto const sub = compat::getSubExprAsWritten(expr); - if (sub->getValueKind() != VK_RValue || isa(sub) - || isa(sub)) + if (sub->getValueKind() != VK_RValue || expr->getType()->isRecordType() + || isa(sub) || isa(sub)) { return true; } diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx index 22130b32196c..78624810fda9 100644 --- a/compilerplugins/clang/test/redundantcast.cxx +++ b/compilerplugins/clang/test/redundantcast.cxx @@ -253,7 +253,7 @@ void testStaticCast() { // class prvalue, non-const: (void) static_cast(nsr()); // expected-error {{static_cast from 'S' prvalue to 'S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}} - /* => */ (void) S(nsr()); //TODO: expected-error {{redundant functional cast from 'S' to 'S' [loplugin:redundantcast]}} + /* => */ (void) S(nsr()); // (void) static_cast(nsr()); (void) static_cast(nsr()); (void) static_cast(nsr()); // expected-error {{static_cast from 'S' prvalue to 'const S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}} @@ -273,6 +273,11 @@ void testStaticCast() { (void) static_cast(csr()); } +void testFunctionalCast() { + (void) int(nir()); // expected-error {{redundant functional cast from 'int' to 'int' [loplugin:redundantcast]}} + (void) S(nsr()); +} + void testCStyleCast() { Enum1 e = (Enum1)Enum1::X; // expected-error {{redundant cstyle cast from 'Enum1' to 'Enum1' [loplugin:redundantcast]}} (void)e; @@ -281,6 +286,7 @@ void testCStyleCast() { int main() { testConstCast(); testStaticCast(); + testFunctionalCast(); testCStyleCast(); } -- cgit