Is it possible add new rule to function return value detection

I wrote a function like “int foobar(){}”, and revoke like " foobar()".
It not hanlding the return value, sometimes caused very weired bug.
Could it possible to add this rule?

  • Developer Edition Version 9.9 (build 65466)
  • Language: C

Hi,

For C there is nothing, as the language has (yet) no standard mechanism to mark functions whose return value must be used.
For C++ we have the rule S5277.
Maybe once C23 is published, we can enable that rule for C as well.

In the meantime, you can use a compiler extension and let the compiler warn you

__attribute__ ((warn_unused_result)) // For GCC or clang
extern int foo();

int main(int argc, char** argv)
{
    foo(); // You will get a warning from the compiler
    return 0;
}

Apparently, you don’t even need such an ugly extension: both gcc & clang understand [[nodiscard]] (I checked on Compiler Explorer).

1 Like