Skip to content

clang: regression - erroneous warning when calling [[nodiscard]] static function through a std::forward #131458

Closed as duplicate of#131410
@elbeno

Description

@elbeno

This code:

#include <utility>

struct A {
    [[nodiscard]] static auto f() {
        return 42;
    }
};

template <typename T>
auto f(T &&t) {
    return std::forward<T>(t).f();
}

auto b = f(A{}) == 42;

https://godbolt.org/z/cxGvxM9bn

Provokes a warning:

<source>:11:12: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
   11 |     return std::forward<T>(t).f();
      |            ^~~~~~~~~~~~~~~ ~
<source>:14:10: note: in instantiation of function template specialization 'f<A>' requested here
   14 | auto b = f(A{}) == 42;
      |          ^
1 warning generated.

This seems to be a regression from clang-19.

This happens when:

  • the function called is static
  • we are calling it through member syntax with an object that is std::forward-ed

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions