1

According to the clang docs I am seeing that warn on unused result should be enabled by default. But, it doesn't seem to work unless I use the function __attribute__((warn_unused_result)).

Example:

int foo(void);

doesn't trigger an unused result warning but:

int foo(void) __attribute__((warn_unused_result));

does trigger the unused result warning, all else being equal.

Is there a way to enable unused result warnings globally for all functions which return a value?

PiRocks
  • 1,708
  • 2
  • 18
  • 29
  • 4
    FYI, the [documentation for the attribute](https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result) ties it to the warning, suggesting it is intentional the warning operate only on functions with the attribute. Enabling it for all functions would be problematic, as `printf`, for example, is commonly used solely for its side effect, and its return value is ignored. – Eric Postpischil Feb 08 '21 at 22:13
  • @EricPostpischil, isn't this kind of pointless then, as you have to manually add it to all your functions, thus introducing a new failed sense of security? – Devolus Feb 08 '21 at 22:23
  • @Devolus: The requirement that you add it to functions in order to enable the warning will not catch errors in failing to add it to functions but it will catch errors in failing to use the marked functions correctly (in regard to not paying attention to the return value). I expect the intent is the attribute should be added to routines whose sole or primary function is to return something via their return value, rather than producing some side effect or returning via memory pointed to by a parameter. – Eric Postpischil Feb 08 '21 at 22:24
  • To Dan's point that the warning "should be enabled by default": it is. You can verify this: the warning disappears when you compile with the `__attribute__((warn_unused_result))` present and with the warning explicitly disabled in the compilation command, `-Wno-unused-result` – Jim Danner Feb 08 '21 at 22:29
  • You can use [`#pragma clang attribute`](https://clang.llvm.org/docs/LanguageExtensions.html#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute) to enable the `warn_unused_result` attribute for a group of declarations. You could wrap this around each of your source files. Would that be what you want? – Nate Eldredge Feb 08 '21 at 23:12
  • Though actually, I can't figure out how to get it to work. `#pragma clang attribute push (__attribute__((warn_unused_result)), apply_to = function) ` results in `error: attribute 'warn_unused_result' can't be applied to 'function'`, which makes no sense because the attribute certainly can be applied to functions. https://godbolt.org/z/YMKbb4 – Nate Eldredge Feb 08 '21 at 23:21
  • Try `#pragma clang attribute push (__attribute__((warn_unused_result)), apply_to = hasType(functionType))`. – Eric Postpischil Feb 08 '21 at 23:29
  • @EricPostpischil this is almost what i need. i need to be able to filter only functions which return something. e.g. void foo1(void); int foo2(void); i need to apply the attribute only to foo2 in the example above. – Dan McLeran Feb 09 '21 at 23:26
  • @NateEldredge that works. i'd like to not have to group function forward decls but it's defintely doable. – Dan McLeran Feb 09 '21 at 23:53

0 Answers0