diff --git a/lib/checkassert.cpp b/lib/checkassert.cpp index ab571b1a6..37bc7082a 100644 --- a/lib/checkassert.cpp +++ b/lib/checkassert.cpp @@ -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) diff --git a/test/testassert.cpp b/test/testassert.cpp index 07fa34d05..64c5c43ea 100644 --- a/test/testassert.cpp +++ b/test/testassert.cpp @@ -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"