Skip to content

[clang] Annotating operator* and operator/ with [[nodiscard]] does not produce a diagnostic message #171031

@H-G-Hristov

Description

@H-G-Hristov

While annotating <complex> with [[nodiscard]] I stumbled upon that annotating the overloads bellow with [[nodiscard]] does not produce the expected diagnostic in Clang 22 but it does in GCC 15. See: https://github.com/llvm/llvm-project/actions/runs/20003026692

operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {

operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {

template <typename T>
void test() {
  const std::complex<T> c;
  const std::complex<T> d;
  c* d;     // NOT OK
  c* T(1);  // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
  T(1) * c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}

  c / d;    // NOT OK
  c / T(1); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
  T(1) / c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions