public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paolo Carlini <paolo.carlini@oracle.com>
To: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Cc: Jason Merrill <jason@redhat.com>
Subject: [C++ Patch] PR 80896 ("[[nodiscard]] is ignored for functions returning references")
Date: Wed, 31 May 2017 12:04:00 -0000	[thread overview]
Message-ID: <875f093a-0b8f-7ddc-cd46-cece5a24cb2f@oracle.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 364 bytes --]

Hi,

this one appears to be a rather simple case of missing diagnostic: in 
convert_to_void we aren't calling maybe_warn_nodiscard when we strip an 
INDIRECT_REF wrapping a CALL_EXPR thus we don't issue the diagnostic 
that we normally provide for plain CALL_EXPRs (eg, for a func returning 
a plain int). Tested x86_64-linux.

Thanks, Paolo.

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


[-- Attachment #2: CL_80896 --]
[-- Type: text/plain, Size: 299 bytes --]

/cp
2017-05-31  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/80896
	* cvt.c (convert_to_void): Possibly call maybe_warn_nodiscard
	for case INDIRECT_REF too in the main switch.

/testsuite
2017-05-31  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/80896
	* g++.dg/cpp1z/nodiscard5.C: New.

[-- Attachment #3: patch_80896 --]
[-- Type: text/plain, Size: 905 bytes --]

Index: testsuite/g++.dg/cpp1z/nodiscard5.C
===================================================================
--- testsuite/g++.dg/cpp1z/nodiscard5.C	(revision 0)
+++ testsuite/g++.dg/cpp1z/nodiscard5.C	(working copy)
@@ -0,0 +1,7 @@
+// PR c++/80896
+// { dg-do compile { target c++11 } }
+
+int x = 42;
+[[nodiscard]] int& func() { return x; }
+
+int main() { func(); }  // { dg-warning "ignoring return value" }
Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 248728)
+++ cp/cvt.c	(working copy)
@@ -1296,6 +1296,8 @@ convert_to_void (tree expr, impl_conv_void implici
                 && !is_reference)
               warning_at (loc, OPT_Wunused_value, "value computed is not used");
             expr = TREE_OPERAND (expr, 0);
+	    if (TREE_CODE (expr) == CALL_EXPR)
+	      maybe_warn_nodiscard (expr, implicit);
           }
 
 	break;

             reply	other threads:[~2017-05-31 12:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-31 12:04 Paolo Carlini [this message]
2017-05-31 19:03 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875f093a-0b8f-7ddc-cd46-cece5a24cb2f@oracle.com \
    --to=paolo.carlini@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).