This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] With -Wuseless-cast on direct enum init (PR c++/82299)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>, Nathan Sidwell <nathan at acm dot org>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Fri, 29 Sep 2017 22:57:25 +0200
- Subject: [C++ PATCH] With -Wuseless-cast on direct enum init (PR c++/82299)
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jakub at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4CC0EC057FA1
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
The casts added for the C++17 direct enum initialization IMNSHO shouldn't
trigger -Wuseless-cast warnings.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?
2017-09-29 Jakub Jelinek <jakub@redhat.com>
PR c++/82299
* decl.c (reshape_init): Suppress warn_useless_cast for direct enum
init.
* typeck.c (convert_for_assignment): Likewise.
* g++.dg/cpp0x/pr82299.C: New test.
--- gcc/cp/decl.c.jj 2017-09-29 11:21:55.000000000 +0200
+++ gcc/cp/decl.c 2017-09-29 15:19:02.549548102 +0200
@@ -6053,7 +6053,10 @@ reshape_init (tree type, tree init, tsub
tree elt = CONSTRUCTOR_ELT (init, 0)->value;
type = cv_unqualified (type);
if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
- return cp_build_c_cast (type, elt, tf_warning_or_error);
+ {
+ warning_sentinel w (warn_useless_cast);
+ return cp_build_c_cast (type, elt, tf_warning_or_error);
+ }
else
return error_mark_node;
}
--- gcc/cp/typeck.c.jj 2017-09-29 09:07:33.000000000 +0200
+++ gcc/cp/typeck.c 2017-09-29 15:21:17.770926572 +0200
@@ -8527,7 +8527,10 @@ convert_for_assignment (tree type, tree
{
tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
- rhs = cp_build_c_cast (type, elt, complain);
+ {
+ warning_sentinel w (warn_useless_cast);
+ rhs = cp_build_c_cast (type, elt, complain);
+ }
else
rhs = error_mark_node;
}
--- gcc/testsuite/g++.dg/cpp0x/pr82299.C.jj 2017-09-29 15:24:18.909754416 +0200
+++ gcc/testsuite/g++.dg/cpp0x/pr82299.C 2017-09-29 15:29:15.589237065 +0200
@@ -0,0 +1,9 @@
+// PR c++/82299
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wuseless-cast" }
+
+enum Enum : char { A = 0, B = 1 };
+
+struct S {
+ Enum e { Enum::A }; // { dg-bogus "useless cast to type" }
+};
Jakub