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 --versioncc (Debian 10.2.1-6) 10.2.1 20210110Copyright (C) 2020 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; 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?...)