3

I had some code that used the GCC extension [[gnu::warn_unused_result]] (a.k.a. __attribute__((__warn_unused_result__))). Now I attempted to use C2x's [[nodiscard]] and I got an incomprehensible error. I'm not sure if the usage of [[nodiscard]] is considerably different from the old GCC attribute, or if it's just a bug in GCC:

$ cat warn_unused_result.c 
[[gnu::warn_unused_result]]
int foo(void);

[[gnu::warn_unused_result]]
int bar(void);


int foo(void)
{
    return 1;
}

int bar(void)
{
    return foo();
}
$ cc -Wall -Wextra -Werror -std=c2x -c warn_unused_result.c
$ cat nodiscard.c 
[[nodiscard]]
int foo(void);

[[nodiscard]]
int bar(void);


int foo(void)
{
    return 1;
}

int bar(void)
{
    return foo();
}
$ cc -Wall -Wextra -Werror -std=c2x -c nodiscard.c          
nodiscard.c:2:1: error: 'nodiscard' attribute directive ignored [-Werror=attributes]
    2 | int foo(void);
      | ^~~
nodiscard.c:5:1: error: 'nodiscard' attribute directive ignored [-Werror=attributes]
    5 | int bar(void);
      | ^~~
cc1: all warnings being treated as errors
$ cc --version
cc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Obviously, I'm not ignoring the return value of any of the functions.

That 1 is never lost (the callers of bar() are in a different translation unit, which might be the problem triggering GCC's behavior?...)

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Ahh, it seems that it is not yet implemented by GCC, and therefore ignored. However, it seems to at least consider it differently than a random attribute such as `[[foobar]]`, because the error is different `attribute directive ignored` vs `attribute ignored`; probably because it is understood to be C++. This is what happens when you attempt to use unreleased features :) – alx - recommends codidact Jul 20 '21 at 22:25
  • 1
    Yeah, I get the same warnings when using [gcc v10.2](https://godbolt.org/z/Y5z9nWT5E), but they disappear when using [gcc v11.1](https://godbolt.org/z/8fYWbTzvj) – yano Jul 20 '21 at 22:28
  • 1
    @yano Thanks! Upgrading to gcc from debian experimental (gcc 11.1.0) silenced the warnings :) – alx - recommends codidact Jul 20 '21 at 22:33
  • However... Now I found an actual bug in `[[nodiscard]]`. Time to report it :) – alx - recommends codidact Jul 20 '21 at 22:36

1 Answers1

1

The difference is that C2X will likely suggest to implementations that the warning can be easily suppressed:

Evaluation of a nodiscard call as a void expression […] is discouraged unless explicitly cast to void.

By contrast, GCC's implementation of attribute warn_unused_result warns even in those cases:

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92