Skip to content

Clang 20 warns on nodiscard-call.static_member_function() #131410

Closed
@timsong-cpp

Description

@timsong-cpp

Clang 20 started issuing warnings on code like

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 db93ef1. I'm surprised that the release notes change did not even mention the impact on warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions