Message ID | e40fb9e2-65bd-677d-5201-5037d2e9ea19@oracle.com |
---|---|
State | New |
Headers | show |
Series | [C++] Do not warn about [[nodiscard]] applied to a constructor | expand |
OK. On Thu, Aug 1, 2019 at 12:36 PM Paolo Carlini <paolo.carlini@oracle.com> wrote: > > 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. > > /////////////////// >
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" } +}