llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open tJener opened this issue 1 year ago • 1 comments

#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.

tJener avatar Jun 02 '25 18:06 tJener

@llvm/issue-subscribers-clang-frontend

Author: Eric Li (tJener)

```c++ #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`.
</details>

llvmbot avatar Jun 03 '25 02:06 llvmbot