Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elbeno opened this issue Mar 15, 2025 · 1 comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer duplicate Resolved as duplicate

Comments

@elbeno
Copy link

elbeno commented Mar 15, 2025

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
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 15, 2025
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Mar 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 15, 2025

@llvm/issue-subscribers-clang-frontend

Author: Ben Deane (elbeno)

This code: ```cpp #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:
```console
&lt;source&gt;:11:12: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
   11 |     return std::forward&lt;T&gt;(t).f();
      |            ^~~~~~~~~~~~~~~ ~
&lt;source&gt;:14:10: note: in instantiation of function template specialization 'f&lt;A&gt;' 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

@EugeneZelenko EugeneZelenko added duplicate Resolved as duplicate clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

4 participants