Re: [PATCH] c++, v2: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666]

2021-05-25 Thread Jason Merrill via Gcc-patches

On 5/25/21 11:10 AM, Jakub Jelinek wrote:

On Tue, May 25, 2021 at 09:40:13AM -0400, Jason Merrill wrote:

Please also test the case of a [[nodiscard]] function returning an empty
class type.


Here it is.  I have also extended the decltype(nullptr) nodiscard test.
Retested on x86_64-linux (nothing other than testcases changed), ok for
trunk?


OK.


2021-05-25  Jakub Jelinek  

PR c++/100666
* call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE
and side-effects, temporarily disable -Wunused-result warning when
building COMPOUND_EXPR.

* g++.dg/cpp1z/nodiscard8.C: New test.
* g++.dg/cpp1z/nodiscard9.C: New test.

--- gcc/cp/call.c.jj2021-05-25 10:55:55.105239017 +0200
+++ gcc/cp/call.c   2021-05-25 17:01:47.524719692 +0200
@@ -8178,7 +8178,10 @@ convert_arg_to_ellipsis (tree arg, tsubs
  {
arg = mark_rvalue_use (arg);
if (TREE_SIDE_EFFECTS (arg))
-   arg = cp_build_compound_expr (arg, null_pointer_node, complain);
+   {
+ warning_sentinel w(warn_unused_result);
+ arg = cp_build_compound_expr (arg, null_pointer_node, complain);
+   }
else
arg = null_pointer_node;
  }
--- gcc/testsuite/g++.dg/cpp1z/nodiscard8.C.jj  2021-05-25 17:01:47.524719692 
+0200
+++ gcc/testsuite/g++.dg/cpp1z/nodiscard8.C 2021-05-25 17:04:34.668904548 
+0200
@@ -0,0 +1,15 @@
+// PR c++/100666
+// { dg-do compile { target c++11 } }
+
+[[nodiscard]] decltype(nullptr) bar ();
+extern void foo (...);
+template  void qux (T);
+
+void
+baz ()
+{
+  foo (bar ());// { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  bar ();  // { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (bar ());// { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+}
--- gcc/testsuite/g++.dg/cpp1z/nodiscard9.C.jj  2021-05-25 17:05:01.907608730 
+0200
+++ gcc/testsuite/g++.dg/cpp1z/nodiscard9.C 2021-05-25 17:06:40.054542742 
+0200
@@ -0,0 +1,22 @@
+// PR c++/100666
+// { dg-do compile { target c++11 } }
+
+struct S {};
+[[nodiscard]] S bar ();
+struct U { S s; };
+[[nodiscard]] U corge ();
+extern void foo (...);
+template  void qux (T);
+
+void
+baz ()
+{
+  foo (bar ());// { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  bar ();  // { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (bar ());// { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  foo (corge ());  // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  corge ();// { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto y = corge ();   // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (corge ());  // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+}


Jakub





[PATCH] c++, v2: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666]

2021-05-25 Thread Jakub Jelinek via Gcc-patches
On Tue, May 25, 2021 at 09:40:13AM -0400, Jason Merrill wrote:
> Please also test the case of a [[nodiscard]] function returning an empty
> class type.

Here it is.  I have also extended the decltype(nullptr) nodiscard test.
Retested on x86_64-linux (nothing other than testcases changed), ok for
trunk?

2021-05-25  Jakub Jelinek  

PR c++/100666
* call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE
and side-effects, temporarily disable -Wunused-result warning when
building COMPOUND_EXPR.

* g++.dg/cpp1z/nodiscard8.C: New test.
* g++.dg/cpp1z/nodiscard9.C: New test.

--- gcc/cp/call.c.jj2021-05-25 10:55:55.105239017 +0200
+++ gcc/cp/call.c   2021-05-25 17:01:47.524719692 +0200
@@ -8178,7 +8178,10 @@ convert_arg_to_ellipsis (tree arg, tsubs
 {
   arg = mark_rvalue_use (arg);
   if (TREE_SIDE_EFFECTS (arg))
-   arg = cp_build_compound_expr (arg, null_pointer_node, complain);
+   {
+ warning_sentinel w(warn_unused_result);
+ arg = cp_build_compound_expr (arg, null_pointer_node, complain);
+   }
   else
arg = null_pointer_node;
 }
--- gcc/testsuite/g++.dg/cpp1z/nodiscard8.C.jj  2021-05-25 17:01:47.524719692 
+0200
+++ gcc/testsuite/g++.dg/cpp1z/nodiscard8.C 2021-05-25 17:04:34.668904548 
+0200
@@ -0,0 +1,15 @@
+// PR c++/100666
+// { dg-do compile { target c++11 } }
+
+[[nodiscard]] decltype(nullptr) bar ();
+extern void foo (...);
+template  void qux (T);
+
+void
+baz ()
+{
+  foo (bar ());// { dg-bogus "ignoring return value of 
'\[^\n\r]*', declared with attribute 'nodiscard'" }
+  bar ();  // { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (bar ());// { dg-bogus "ignoring return value of 
'\[^\n\r]*', declared with attribute 'nodiscard'" }
+}
--- gcc/testsuite/g++.dg/cpp1z/nodiscard9.C.jj  2021-05-25 17:05:01.907608730 
+0200
+++ gcc/testsuite/g++.dg/cpp1z/nodiscard9.C 2021-05-25 17:06:40.054542742 
+0200
@@ -0,0 +1,22 @@
+// PR c++/100666
+// { dg-do compile { target c++11 } }
+
+struct S {};
+[[nodiscard]] S bar ();
+struct U { S s; };
+[[nodiscard]] U corge ();
+extern void foo (...);
+template  void qux (T);
+
+void
+baz ()
+{
+  foo (bar ());// { dg-bogus "ignoring return value of 
'\[^\n\r]*', declared with attribute 'nodiscard'" }
+  bar ();  // { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (bar ());// { dg-bogus "ignoring return value of 
'\[^\n\r]*', declared with attribute 'nodiscard'" }
+  foo (corge ());  // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  corge ();// { dg-warning "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  auto y = corge ();   // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+  qux (corge ());  // { dg-bogus "ignoring return value of '\[^\n\r]*', 
declared with attribute 'nodiscard'" }
+}


Jakub