diff options
-rw-r--r-- | gcc/cp/typeck.cc | 12 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 13 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wuseless-cast.C | 12 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wuseless-cast3.C | 29 |
4 files changed, 54 insertions, 12 deletions
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index da0e142..16e7d85 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8104,11 +8104,13 @@ maybe_warn_about_useless_cast (location_t loc, tree type, tree expr, if (warn_useless_cast && complain & tf_warning) { - if ((TYPE_REF_P (type) - && (TYPE_REF_IS_RVALUE (type) - ? xvalue_p (expr) : lvalue_p (expr)) - && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) - || same_type_p (TREE_TYPE (expr), type)) + if (TYPE_REF_P (type) + ? ((TYPE_REF_IS_RVALUE (type) + ? xvalue_p (expr) : lvalue_p (expr)) + && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) + /* Don't warn when converting a class object to a non-reference type, + because that's a common way to create a temporary. */ + : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type))) warning_at (loc, OPT_Wuseless_cast, "useless cast to type %q#T", type); } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index c176e2d..cd4d3c1 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -4551,7 +4551,18 @@ pointers after reallocation. @item -Wuseless-cast @r{(C++ and Objective-C++ only)} @opindex Wuseless-cast @opindex Wno-useless-cast -Warn when an expression is casted to its own type. +Warn when an expression is cast to its own type. This warning does not +occur when a class object is converted to a non-reference type as that +is a way to create a temporary: + +@smallexample +struct S @{ @}; +void g (S&&); +void f (S&& arg) +@{ + g (S(arg)); // make arg prvalue so that it can bind to S&& +@} +@end smallexample @item -Wno-conversion-null @r{(C++ and Objective-C++ only)} @opindex Wconversion-null diff --git a/gcc/testsuite/g++.dg/warn/Wuseless-cast.C b/gcc/testsuite/g++.dg/warn/Wuseless-cast.C index 2fd6bc4..d7cb899 100644 --- a/gcc/testsuite/g++.dg/warn/Wuseless-cast.C +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast.C @@ -62,11 +62,11 @@ A prvalue(); void f() { - int n; + int n; - (int)(n); // { dg-warning "3:useless cast" } - static_cast<int>(n); // { dg-warning "3:useless cast" } - reinterpret_cast<int>(n); // { dg-warning "3:useless cast" } + (int)(n); + static_cast<int>(n); + reinterpret_cast<int>(n); (int*)(&n); // { dg-warning "3:useless cast" } const_cast<int*>(&n); // { dg-warning "3:useless cast" } @@ -100,8 +100,8 @@ void f() A a; - (A)(a); // { dg-warning "3:useless cast" } - static_cast<A>(a); // { dg-warning "3:useless cast" } + (A)(a); + static_cast<A>(a); (A*)(&a); // { dg-warning "3:useless cast" } const_cast<A*>(&a); // { dg-warning "3:useless cast" } diff --git a/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C b/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C new file mode 100644 index 0000000..43dd2eb --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C @@ -0,0 +1,29 @@ +// PR c++/85043 +// { dg-do compile { target c++11 } } +// { dg-options "-Wuseless-cast" } + +struct S { int s; void bump () { s++; } }; + +void +foo () +{ + S s = { 1 }; + s.bump (); + S (s).bump (); // { dg-bogus "useless" } + ((S) s).bump (); // { dg-bogus "useless" } + static_cast<S> (s).bump (); // { dg-bogus "useless" } +} + +struct X { }; +void g(X&&); + +void +f (X&& arg) +{ + g(X(arg)); // { dg-bogus "useless" } + g(X(X{})); // { dg-warning "useless" } + g(static_cast<X&&>(arg)); + + int i = (int) 1; // { dg-warning "useless" } + const int &r = (int) i; // { dg-bogus "useless" } +} |