Created
May 7, 2022 11:06
-
-
Save marcobergamin/7760b39cfc1af25af6a858fcd82a77ea to your computer and use it in GitHub Desktop.
An example of the clang-tidy check: bugprone-assert-side-effect
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
#include <cstdlib> | |
#ifndef NDEBUG | |
#define my_assert(x) { x ? static_cast<void>(x) : std::abort(); } | |
#else | |
#define my_assert(x) static_cast<void>(x) | |
#endif | |
class Foo { | |
public: | |
int function_with_side_effects() { | |
state_ = 1; | |
return 0; | |
} | |
int function_with_no_side_effects() const { | |
return state_; | |
} | |
private: | |
int state_{0}; | |
}; | |
int main() { | |
Foo foo{}; | |
my_assert(foo.function_with_side_effects() == 0 && "Should never fail"); | |
my_assert(foo.function_with_no_side_effects() == 1); | |
return 0; | |
} | |
// Command: | |
// clang-tidy ../main.cpp --config="{Checks: 'bugprone-assert-side-effect', CheckOptions: \ | |
// [{key: bugprone-assert-side-effect.CheckFunctionCalls, value: 1}, \ | |
// {key: bugprone-assert-side-effect.AssertMacros, value: 'assert,my_assert'}]}" | |
// | |
// Output: | |
// /main.cpp:26:15: error: side effect in my_assert() condition discarded in release builds [bugprone-assert-side-effect,-warnings-as-errors] | |
// my_assert(foo.function_with_side_effects() == 0 && "Should never fail"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment