Last active
May 12, 2018 16:54
-
-
Save vietlq/7e3d1aecb595b6c1ba0d0d09d333d47c to your computer and use it in GitHub Desktop.
C++ (GCC / Clang): Warn on unused function result/return for safety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html | |
// https://infektor.net/posts/2017-01-19-using-cpp17-attributes-today.html | |
// Compiler flags: -Wall -Werror -Wextra -pedantic -Wunused-result -std=c++11 | |
// Suggested macro names: HANDLE_OUTPUT / NODISCARD / MUST_HANDLE | |
#define HANDLE_OUTPUT __attribute__((warn_unused_result)) | |
HANDLE_OUTPUT int critical_func() | |
{ | |
return 1234; | |
} | |
template<typename ... Args> | |
void ignore(Args && ...) | |
{ | |
} | |
int use_critical_func() | |
{ | |
// This will cause the warning: [x86-64 gcc 7.2 #1] error: ignoring return value of 'int critical_func()', | |
// declared with attribute warn_unused_result [-Werror=unused-result] | |
//critical_func(); | |
// This will pass | |
ignore(critical_func()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment