https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98019
Bug ID: 98019
Summary: Concepts: compound requirement expression from
'requires' expression is considered discarded-value
expression for [[nodiscard]], false positive warning
emitted
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: d
Assignee: ibuclaw at gdcproject dot org
Reporter: egor_suvorov at mail dot ru
Target Milestone: ---
The following code:
#include <concepts>
[[nodiscard]] int foo() {
return 0;
}
[[maybe_unused]] constexpr bool b = requires {
{ foo() } -> std::same_as<int>;
};
[[maybe_unused]] constexpr auto x = sizeof(foo());
gets the following warning at gcc trunk and gcc 10.2 with `-std=c++20`:
<source>:6:10: warning: ignoring return value of 'int foo()', declared with
attribute 'nodiscard' [-Wunused-result]
6 | { foo() } -> std::same_as<int>;
| ~~~^~
<source>:2:19: note: declared here
2 | [[nodiscard]] int foo() {
| ^~~
However, `foo()` here is an non-evaluated expression, not a statement which
discards the value. It's highlighted by the use of optional
return-type-requirement. Even if it was not there, I would still not consider
this a warning-worthy.
Note that usage of `foo()` inside `sizeof()` (another non-evaluated context)
does not emit the same warning.
Corresponding Godbolt link: https://godbolt.org/z/aKx3hz