Hi,

in Cologne, during the presentation of P1771R0, Per Sommerlad pointed out that apparently GCC was already almost doing the right thing - it accepts [[nodiscard]] on a constructor and then a warning is emitted in the relevant potentially dangerous situations - but it does first emit a warning when it encounters the [[nodiscard]] itself. Avoiding the latter seems easy to me - the below passes testing. Something else?

Thanks, Paolo.

///////////////////

/cp
2019-08-01  Paolo Carlini  <paolo.carl...@oracle.com>

        * tree.c (handle_nodiscard_attribute): Do not warn about nodiscard
        applied to a constructor.

/testsuite
2019-08-01  Paolo Carlini  <paolo.carl...@oracle.com>

        * g++.dg/cpp1z/nodiscard6.C: New.
Index: cp/tree.c
===================================================================
--- cp/tree.c   (revision 273951)
+++ cp/tree.c   (working copy)
@@ -4361,7 +4361,8 @@ handle_nodiscard_attribute (tree *node, tree name,
 {
   if (TREE_CODE (*node) == FUNCTION_DECL)
     {
-      if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
+      if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
+         && !DECL_CONSTRUCTOR_P (*node))
        warning_at (DECL_SOURCE_LOCATION (*node),
                    OPT_Wattributes, "%qE attribute applied to %qD with void "
                    "return type", name, *node);
Index: testsuite/g++.dg/cpp1z/nodiscard6.C
===================================================================
--- testsuite/g++.dg/cpp1z/nodiscard6.C (nonexistent)
+++ testsuite/g++.dg/cpp1z/nodiscard6.C (working copy)
@@ -0,0 +1,11 @@
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+  [[nodiscard]] A();
+};
+
+void foo()
+{
+  A();  // { dg-warning "ignoring return value" }
+}

Reply via email to