PR c++/89070 - bogus [[nodiscard]] warning in SFINAE.
authorMarek Polacek <polacek@redhat.com>
Wed, 13 Nov 2019 15:59:53 +0000 (15:59 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 13 Nov 2019 15:59:53 +0000 (15:59 +0000)
This is a complaint that we issue a [[nodiscard]] warning even in SFINAE
contexts.  Here 'complain' is tf_decltype, but not tf_warning so I guess
we can fix it as below.

* cvt.c (convert_to_void): Guard maybe_warn_nodiscard calls with
tf_warning.

* g++.dg/cpp1z/nodiscard7.C: New test.

From-SVN: r278147

gcc/cp/ChangeLog
gcc/cp/cvt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1z/nodiscard7.C [new file with mode: 0644]

index 5dcf13bbc89c23dbe35e845b5c5dd4a5099c22bc..5c9c9f1a7bf92808bd054fe377808c4a87ead631 100644 (file)
@@ -1,3 +1,9 @@
+2019-11-13  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/89070 - bogus [[nodiscard]] warning in SFINAE.
+       * cvt.c (convert_to_void): Guard maybe_warn_nodiscard calls with
+       tf_warning.
+
 2019-11-13  Richard Sandiford  <richard.sandiford@arm.com>
 
        PR c++/92206
index d41aeb8f1fc9dfd260fc6c14bcef6aef0a9575a7..23facb77634f2f49854560ab26ddf9131636089b 100644 (file)
@@ -1201,7 +1201,8 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
        if (DECL_DESTRUCTOR_P (fn))
          return expr;
 
-      maybe_warn_nodiscard (expr, implicit);
+      if (complain & tf_warning)
+       maybe_warn_nodiscard (expr, implicit);
       break;
 
     case INDIRECT_REF:
@@ -1357,7 +1358,8 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
                 && !is_reference)
               warning_at (loc, OPT_Wunused_value, "value computed is not used");
             expr = TREE_OPERAND (expr, 0);
-           if (TREE_CODE (expr) == CALL_EXPR)
+           if (TREE_CODE (expr) == CALL_EXPR
+               && (complain & tf_warning))
              maybe_warn_nodiscard (expr, implicit);
           }
 
@@ -1435,7 +1437,8 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
                                           AGGR_INIT_EXPR_ARGP (init));
            }
        }
-      maybe_warn_nodiscard (expr, implicit);
+      if (complain & tf_warning)
+       maybe_warn_nodiscard (expr, implicit);
       break;
 
     default:;
index 9e062e799d31ad70fbdefdc33a93de2b67301228..8907e582219d64f7c42c6be6e2f9ced1d87f26c7 100644 (file)
@@ -1,3 +1,8 @@
+2019-11-13  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/89070 - bogus [[nodiscard]] warning in SFINAE.
+       * g++.dg/cpp1z/nodiscard7.C: New test.
+
 2019-11-13  Joseph Myers  <joseph@codesourcery.com>
 
        * gcc.dg/c11-float-3.c, gcc.dg/c2x-float-1.c: New tests.
diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard7.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard7.C
new file mode 100644 (file)
index 0000000..80dac63
--- /dev/null
@@ -0,0 +1,18 @@
+// PR c++/89070 - bogus [[nodiscard]] warning in SFINAE.
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+    [[nodiscard]] static int match() { return 42; }
+};
+
+template<typename T>
+auto g() -> decltype( T::match(), bool() )
+{
+    return T::match();
+}
+
+int main()
+{
+    g<A>();
+}