Skip to content

Add warning for coroutines without any co_* keywords in the definition #171023

@JankoDedic

Description

@JankoDedic

This is a feature request for a Clang C++ compiler warning.

A function without any co_* keywords in its body is not a coroutine, even if it returns an awaitable type. What is intended to be a coroutine by the developer can accidentally become a normal function, which has entirely different behavior and can result in surprising bugs and undefined behavior. Some of these mistakes will be caught by -Wreturn-type, but not all of them.

Example: https://godbolt.org/z/8EfeTYdvq

#include <coroutine>

struct coroutine {
    struct promise_type {
        coroutine get_return_object() { return {}; }
        std::suspend_always initial_suspend() { return {}; }
        void return_void() {}
        void unhandled_exception() {}
        std::suspend_always final_suspend() noexcept { return {}; }
    };
};

coroutine example() {
    throw 67;
}

A compiler warning diagnosing functions with awaitable return types that don't have any co_* keywords in the definition could help entirely eliminate this class of bugs from codebases using coroutines.

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 coroutines

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions