0
votes

I'm trying to use Visual Studio 2017 Code Analysis to find places where the return values from a function are ignored.

I'm trying to avoid using the SAL annotation: _Check_return_. It makes the code less readable and requires a lot of effort to add it to all the functions.

_Check_return_ int  foo(_In_ int x){return x + 1;}

void goo(){foo(5);}

This will produce a warning:

Warning C6031 Return value ignored: 'foo'

Is there a simple way to get the same result without explicitly demanding it?

1
[[nodiscard]] C++17 attribute can help. - Jovibor

1 Answers

0
votes

I suggest to consider using more sophisticated code analyzers, such as PVS-Studio. For the code without mark up:

int  foo(_In_ int x) {return x + 1;}
void goo() {foo(5);}

PVS-Studio issues the warning V530 [CWE-252] The return value of function 'foo' is required to be utilized. ConsoleApplication1.cpp 23

The V530 diagnostic relies on markup of thousands of functions annotated inside the analyzer. Also the annotations written manually in the code are also taken into account. Moreover, the analyzer can automatically build annotations and understands that there is no point to call foo and not to use its result, as the function hasn't got any side effect - the function doesn't print on the screen, doesn't change global variables and so on. That's why the analyzer issues the V530 warning for the code above.