UPDATE FOR MSVC 2012 AND AFTER
Many thanks to @Albert for pointing out that MSVC now supports _Check_return_ annotation compared to Visual Studio 2012 when using static SAL code analysis. I am adding this answer to include a cross-platform macro that may be useful to others:
#if defined(__GNUC__) && (__GNUC__ >= 4) #define CHECK_RESULT __attribute__ ((warn_unused_result)) #elif defined(_MSC_VER) && (_MSC_VER >= 1700) #define CHECK_RESULT _Check_return_ #else #define CHECK_RESULT #endif
Note that, unlike gcc and others, (a) MSVC requires annotations for both the declaration and the definition of the function, and (b) the annotation should be at the beginning of the declaration / definition (gcc allows either). Therefore, the use should usually be, for example:
// foo.c CHECK_RETURN int my_function(void) // definition { return 42; }
Also note that to compile from the command line you will need /analyze (or -analyze ), or the equivalent if you are using the Visual Studio IDE. It also slows down the build process.
Paul R Mar 31 '14 at 10:33 2014-03-31 10:33
source share