Bug 89070 - Attribute [[nodiscard]] should be ignored in unevaluated contexts
Summary: Attribute [[nodiscard]] should be ignored in unevaluated contexts
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
: 92477 (view as bug list)
Depends on:
Blocks:
 
Reported: 2019-01-26 08:37 UTC by Daniel Frey
Modified: 2019-11-13 16:04 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-01-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Frey 2019-01-26 08:37:39 UTC
Title says it all. In an unevaluated context, the result is never used anyways. Other compilers do ignore [[nodiscard]] in those contexts already. Example code:

struct A
{
    [[nodiscard]] static int match() { return 42; }
};

template<typename T>
auto g() -> decltype( T::match(), bool() )
{
    return T::match();
}

int main()
{
    g<A>();
}

Leads to:

<source>: In substitution of 'template<class T> decltype ((T::match(), bool())) g() [with T = A]':

<source>:14:10:   required from here

<source>:7:31: warning: ignoring return value of 'static int A::match()', declared with attribute nodiscard [-Wunused-result]

    7 | auto g() -> decltype( T::match(), bool() )

      |                       ~~~~~~~~^~

<source>:3:30: note: declared here

    3 |     [[nodiscard]] static int match() { return 42; }

      |                              ^~~~~

Compiler returned: 0

See https://godbolt.org/z/OQEnaD
Comment 1 Martin Sebor 2019-01-29 01:03:44 UTC
The problem is specific to [[nodiscard]] and doesn't affect attribute warn_unused_result (either the traditional form or [[gnu::warn_unused_result]]).
Comment 2 Jonathan Wakely 2019-11-12 15:06:59 UTC
*** Bug 92477 has been marked as a duplicate of this bug. ***
Comment 3 Marek Polacek 2019-11-13 16:00:26 UTC
Author: mpolacek
Date: Wed Nov 13 15:59:53 2019
New Revision: 278147

URL: https://gcc.gnu.org/viewcvs?rev=278147&root=gcc&view=rev
Log:
	PR c++/89070 - bogus [[nodiscard]] warning in SFINAE.

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.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1z/nodiscard7.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cvt.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2019-11-13 16:04:11 UTC
Fixed.