Issue 131410
Summary Clang 20 warns on nodiscard-call.static_member_function()
Labels clang
Assignees
Reporter timsong-cpp
    Clang 20 started issuing warnings on code like

```cpp
struct mapping {
 static bool is_exhaustive();
};
template<class M>
struct mdspan {
 [[nodiscard]] M const& mapping() const;
};

void f() {
 mdspan<mapping> s;
    s.mapping().is_exhaustive();  // warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
}
```

Whatever the standard says about discarded-value expressions, warning on this code - basically a simplified `std::mdspan` - is undesirable. The actual `mapping()` member is marked nodiscard in at least one standard library implementation.

Not only is there nothing particularly wrong about calling a static member function like this, but here `is_exhaustive()` is a static member only as an implementation detail of the specific `mapping` class (in a different class, the result can depend on the state of `*this` and hence it needs to be non-static), so that it would be _wrong_ for the user to call it statically in generic code. There's also no easy way to suppress the warning. 

So the only way for the author of a template like this is to not mark `mapping()` as `[[nodiscard]]`. Which, it should go without saying, is rather unfortunate. `s.mapping();` should trigger a warning because the user probably meant something else.

This is presumably introduced by db93ef14aef9c572e02bc842762bc4d0278148f9. I'm surprised that the release notes change did not even mention the impact on warnings.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to