voidcheck_(constchar* file, int line, constchar (&code)[size]) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
CheckAssert check;
runChecks(check, tokenizer, *this);
}
voidrun() override {
mNewTemplate = true;
TEST_CASE(assignmentInAssert);
TEST_CASE(functionCallInAssert);
TEST_CASE(memberFunctionCallInAssert);
TEST_CASE(safeFunctionCallInAssert);
TEST_CASE(crash);
}
voidsafeFunctionCallInAssert() {
check(
"int a;\n"
"bool b = false;\n"
"int foo() {\n"
" if (b) { a = 1+2 };\n"
" return a;\n"
"}\n"
"assert(foo() == 3);");
ASSERT_EQUALS("", errout_str());
check(
"int foo(int a) {\n"
" int b=a+1;\n"
" return b;\n"
"}\n"
"assert(foo(1) == 2);");
ASSERT_EQUALS("", errout_str());
}
voidfunctionCallInAssert() {
check(
"int a;\n"
"int foo() {\n"
" a = 1+2;\n"
" return a;\n"
"}\n"
"assert(foo() == 3);");
ASSERT_EQUALS("[test.cpp:6:8]: (warning) Assert statement calls a function which may have desired side effects: 'foo'. [assertWithSideEffect]\n", errout_str());
// Ticket #4937 "false positive: Assert calls a function which may have desired side effects"
check("struct SquarePack {\n"
" static bool isRank1Or8( Square sq ) {\n"
" sq &= 0x38;\n"
" return sq == 0 || sq == 0x38;\n"
" }\n"
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct SquarePack {\n"
" static bool isRank1Or8( Square &sq ) {\n"
" sq &= 0x38;\n"
" return sq == 0 || sq == 0x38;\n"
" }\n"
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}");
ASSERT_EQUALS("[test.cpp:8:25]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'. [assertWithSideEffect]\n", errout_str());
check("struct SquarePack {\n"
" static bool isRank1Or8( Square *sq ) {\n"
" *sq &= 0x38;\n"
" return *sq == 0 || *sq == 0x38;\n"
" }\n"
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}");
ASSERT_EQUALS("[test.cpp:8:25]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'. [assertWithSideEffect]\n", errout_str());
check("struct SquarePack {\n"
" static bool isRank1Or8( Square *sq ) {\n"
" sq &= 0x38;\n"
" return sq == 0 || sq == 0x38;\n"
" }\n"
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct Geometry {\n"
" int nbv;\n"
" int empty() { return (nbv == 0); }\n"
" void ReadGeometry();\n"
"};\n"
"\n"
"void Geometry::ReadGeometry() {\n"
" assert(empty());\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"// #4811
" void f() const;\n"
" bool g(std::ostream& os = std::cerr) const;\n"
"};\n"
"void S::f() const {\n"
" assert(g());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int i;\n"// #14973
"bool f() {\n"
" i = 0;\n"
" return true;\n"
"}\n"
"void g() {\n"
" bool (*fp)() = f;\n"
" assert(fp == f);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
voidmemberFunctionCallInAssert() {
check("struct SquarePack {\n"
" void Foo();\n"
"};\n"
"void foo(SquarePack s) {\n"
" assert( s.Foo() );\n"
"}");
ASSERT_EQUALS("[test.cpp:5:14]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'. If there are no side effects, consider declaring the method const. [assertWithSideEffect]\n", errout_str());