Fixed #9926 (False positive assertWithSideEffects, calling method that has no side effects)

This commit is contained in:
Daniel Marjamäki 2022-10-12 07:51:50 +02:00
parent 3e16367a97
commit b8b6be48d9
2 changed files with 18 additions and 6 deletions

View File

@ -59,12 +59,13 @@ void CheckAssert::assertWithSideEffects()
continue;
const Function* f = tmp->function();
if (f->nestedIn->isClassOrStruct() && !f->isStatic() && !f->isConst()) {
sideEffectInAssertError(tmp, f->name()); // Non-const member function called
const Scope* scope = f->functionScope;
if (!scope) {
// guess that const method doesn't have side effects
if (f->nestedIn->isClassOrStruct() && !f->isConst() && !f->isStatic())
sideEffectInAssertError(tmp, f->name()); // Non-const member function called, assume it has side effects
continue;
}
const Scope* scope = f->functionScope;
if (!scope) continue;
for (const Token *tok2 = scope->bodyStart; tok2 != scope->bodyEnd; tok2 = tok2->next()) {
if (!tok2->isAssignmentOp() && tok2->tokType() != Token::eIncDecOp)

View File

@ -133,6 +133,17 @@ private:
" 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());
}
void memberFunctionCallInAssert() {
@ -145,7 +156,7 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'.\n", errout.str());
check("struct SquarePack {\n"
" void Foo() const;\n"
" int Foo() const;\n"
"};\n"
"void foo(SquarePack* s) {\n"
" assert( s->Foo() );\n"
@ -153,7 +164,7 @@ private:
ASSERT_EQUALS("", errout.str());
check("struct SquarePack {\n"
" static void Foo();\n"
" static int Foo();\n"
"};\n"
"void foo(SquarePack* s) {\n"
" assert( s->Foo() );\n"