hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.39k stars 232 forks source link

[BUG] Emitted prefix increment and decrement should not be `[[nodiscard]]` #882

Closed JohelEGP closed 8 months ago

JohelEGP commented 8 months ago

Follow up commits 41e54690f914ece7bc70e2ae695dd183ebf1fd53, db63152657031bdd127144750a3990b34105d8b9 and 533b62d2c46e5efe57a569c4ee1e890536de671c by also making prefix increment and decrement not [[nodiscard]].

See https://github.com/hsutter/cppfront/issues/231#issuecomment-1657276427:

I might have an use case for marking an ignorable output on a function declaration.

Consider https://compiler-explorer.com/z/r6sq4nGM1, generated from https://cpp2.godbolt.org/z/K3v7xWeav.

The iterator::operator++ returns this. MSVC and GCC warn on the unused result of the compiler rewrite of the range for statement.

iterator: @struct @ordered type = {
  operator*: (this) -> i32 = 0;
  operator++: (this) -> forward iterator = this;
}
range: @struct type = {
  begin: (this) -> iterator = ();
  end: (this) -> iterator = ();
}
main: () = {
  for range() do (x) _ = x;
}
main.cpp2: In function 'int main()':
main.cpp2:10:31: error: ignoring return value of 'const iterator& iterator::operator++() const', declared with attribute 'nodiscard' [-Werror=unused-result]
   10 |   for range() do (x) _ = x;
      |                               ^
main.cpp2:3:22: note: declared here
    3 |   operator++: (this) -> forward iterator = this;
      |                      ^~~~~~~~
cc1plus: some warnings being treated as errors