Skip to content

clang incorrectly warns about returning reference to local #112021

Open
@kelbon

Description

@kelbon

pipeline ref with warn:

https://github.com/kelbon/kelcoro/actions/runs/11295888979/job/31419583993?pr=31

warning:

kelcoro/include/kelcoro/task.hpp:176:12: warning: returning reference to local temporary object [-Wreturn-stack-address]
  176 |     return [](task t) -> async_task<result_type> { co_return co_await t; }(std::move(*this)).get();
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/kelcoro/kelcoro/tests/test_coroutines.cpp:817:29: note: in instantiation of member function 'dd::task<int &>::get' requested here
  817 |   error_if(&rvo_task_ref(x).get() != &x);
      |                             ^
1 warning generated.

code:

dd::task<int&> rvo_task_ref(int& x) {
  (void)co_await jump_on(TP);
  int*& ret = co_await dd::this_coro::return_place;
  ret = &x;
  co_return dd::rvo;
}

TEST(rvo_tasks) {
  int x = 0;
  error_if(&rvo_task_ref(x).get() != &x);
  return error_count;
}

Description of what happens:

task stores int* in its promise, local variable in TEST goes into coroutine by reference (and reference stored on coroutine frame)
Then, .get creates another coroutine and does get (blocking wait for result), returns int&.

Compiler for some reasons thinks, that reference to 'x' is dangling, seems for me, that its incorrect

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzercoroutinesC++20 coroutinesincompleteIssue not complete (e.g. missing a reproducer, build arguments, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions