Skip to content

[clang] [[nodiscard]] on return type of function pointer is ignored sometimes #142453

Open
@tJener

Description

@tJener
#include <functional>
#include <utility>

struct [[nodiscard]] S {};
S (*get_fp())();

template <typename Func>
void f(Func fn) {
    static_cast<Func>(fn)();    // Ignored
    static_cast<Func&>(fn)();   // Ignored
    static_cast<Func&&>(fn)();  // Ignored
    std::move(fn)();            // Ignored
    fn();
}

void i() {
    f(get_fp());
    f(std::bind(get_fp()));

    get_fp()();  // Ignored
    std::bind(get_fp())();
}

https://godbolt.org/z/Mje7nWh1r


When a function pointer returning a type marked [[nodiscard]] is invoked, it will sometimes not generate a -Wunused-value warning. As a comparison, it will generate the warning if wrapped in a std::bind.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions