Skip to content

Commit b8b6be4

Browse files
committed
Fixed #9926 (False positive assertWithSideEffects, calling method that has no side effects)
1 parent 3e16367 commit b8b6be4

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

lib/checkassert.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ void CheckAssert::assertWithSideEffects()
5959
continue;
6060

6161
const Function* f = tmp->function();
62-
if (f->nestedIn->isClassOrStruct() && !f->isStatic() && !f->isConst()) {
63-
sideEffectInAssertError(tmp, f->name()); // Non-const member function called
62+
const Scope* scope = f->functionScope;
63+
if (!scope) {
64+
// guess that const method doesn't have side effects
65+
if (f->nestedIn->isClassOrStruct() && !f->isConst() && !f->isStatic())
66+
sideEffectInAssertError(tmp, f->name()); // Non-const member function called, assume it has side effects
6467
continue;
6568
}
66-
const Scope* scope = f->functionScope;
67-
if (!scope) continue;
6869

6970
for (const Token *tok2 = scope->bodyStart; tok2 != scope->bodyEnd; tok2 = tok2->next()) {
7071
if (!tok2->isAssignmentOp() && tok2->tokType() != Token::eIncDecOp)

test/testassert.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ class TestAssert : public TestFixture {
133133
" assert( !SquarePack::isRank1Or8(push2) );\n"
134134
"}");
135135
ASSERT_EQUALS("", errout.str());
136+
137+
check("struct Geometry {\n"
138+
" int nbv;\n"
139+
" int empty() { return (nbv == 0); }\n"
140+
" void ReadGeometry();\n"
141+
"};\n"
142+
"\n"
143+
"void Geometry::ReadGeometry() {\n"
144+
" assert(empty());\n"
145+
"}");
146+
ASSERT_EQUALS("", errout.str());
136147
}
137148

138149
void memberFunctionCallInAssert() {
@@ -145,15 +156,15 @@ class TestAssert : public TestFixture {
145156
ASSERT_EQUALS("[test.cpp:5]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'.\n", errout.str());
146157

147158
check("struct SquarePack {\n"
148-
" void Foo() const;\n"
159+
" int Foo() const;\n"
149160
"};\n"
150161
"void foo(SquarePack* s) {\n"
151162
" assert( s->Foo() );\n"
152163
"}");
153164
ASSERT_EQUALS("", errout.str());
154165

155166
check("struct SquarePack {\n"
156-
" static void Foo();\n"
167+
" static int Foo();\n"
157168
"};\n"
158169
"void foo(SquarePack* s) {\n"
159170
" assert( s->Foo() );\n"

0 commit comments

Comments
 (0)