https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116502

            Bug ID: 116502
           Summary: [15 Regression] -Wunused-result warning is not
                    supressed if coroutine awaiter returns a reference
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: c++-coroutines
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daklishch at gmail dot com
                CC: arsen at gcc dot gnu.org
  Target Milestone: ---

After 2664c1bf83855b79d8c43772e71b87ed4f09c174 (cp+coroutines: teach
convert_to_void to diagnose discarded co_awaits), the following code produces
-Wunused-result warning, while the caller clearly wants to suppress it by
casting the co_await result to void.

```
$ cat ../test.cpp
#include <coroutine>

struct SuspendNever {
    bool await_ready() noexcept;
    void await_suspend(std::coroutine_handle<>) noexcept;
    void await_resume() noexcept;
};

struct Coroutine;

struct PromiseType {
    Coroutine get_return_object();
    SuspendNever initial_suspend();
    SuspendNever final_suspend() noexcept;
    void return_void();
};

struct Coroutine {
    using promise_type = PromiseType;
};

struct Awaiter {
    bool await_ready();
    void await_suspend(std::coroutine_handle<>);
    [[nodiscard]] int& await_resume();
};

Coroutine foo()
{
    (void)co_await Awaiter {};
}

$ ~/gcc-15-trunk/bin/g++ ../test.cpp -c -std=c++20 -fno-exceptions
-Wunused-result -o /dev/null
../test.cpp: In function ‘Coroutine foo()’:
../test.cpp:30:29: warning: ignoring return value of ‘int&
Awaiter::await_resume()’, declared with attribute ‘nodiscard’ [-Wunused-result]
   30 |     (void)co_await Awaiter {};
      |                             ^
../test.cpp:25:24: note: declared here
   25 |     [[nodiscard]] int& await_resume();
      |                        ^~~~~~~~~~~~
```

The warning is correctly suppressed unless Awaiter::await_resume returns a
reference.

Reply via email to