public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10614] c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] Date: Tue, 10 May 2022 08:18:12 +0000 (GMT) [thread overview] Message-ID: <20220510081812.B1F1438346BA@sourceware.org> (raw) https://gcc.gnu.org/g:589a0f34b1d176b62b064c9815c341b6abf48a2b commit r10-10614-g589a0f34b1d176b62b064c9815c341b6abf48a2b Author: Jakub Jelinek <jakub@redhat.com> Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek <jakub@redhat.com> 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. (cherry picked from commit ad52d89808a947264397e920d7483090d4108f7b) Diff: --- gcc/cp/call.c | 5 ++++- gcc/testsuite/g++.dg/cpp1z/nodiscard8.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/cpp1z/nodiscard9.C | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gcc/cp/call.c b/gcc/cp/call.c index f636ef4cb03..29e4a52b6c4 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -7968,7 +7968,10 @@ convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain) { 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; } diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C new file mode 100644 index 00000000000..b5096acbea1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C @@ -0,0 +1,15 @@ +// PR c++/100666 +// { dg-do compile { target c++11 } } + +[[nodiscard]] decltype(nullptr) bar (); +extern void foo (...); +template <typename T> 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'" } +} diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C new file mode 100644 index 00000000000..1315ccdbf7c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C @@ -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 <typename T> 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'" } +}
reply other threads:[~2022-05-10 8:18 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20220510081812.B1F1438346BA@sourceware.org \ --to=jakub@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /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: linkBe 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).