llvm-project
llvm-project copied to clipboard
[clang] [[nodiscard]] on return type of function pointer is ignored sometimes
#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.
@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>