Skip to content

Commit aa25d1e

Browse files
committed
Fixed cppcheck-opensource#4837 (False positive: Assert statement calls a function which may have desired side effects (local variable))
1 parent 8862864 commit aa25d1e

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/checkassert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void CheckAssert::assertWithSideEffects()
6868
for (const Token *tok2 = scope->classStart; tok2 != scope->classEnd; tok2 = tok2->next()) {
6969
if (tok2->type() != Token::eAssignmentOp && tok2->type() != Token::eIncDecOp) continue;
7070
const Variable* var = tok2->previous()->variable();
71-
if (!var) continue;
71+
if (!var || var->isLocal()) continue;
7272

7373
std::vector<const Token*> returnTokens; // find all return statements
7474
for (const Token *rt = scope->classStart; rt != scope->classEnd; rt = rt->next()) {

test/testassert.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ class TestAssert : public TestFixture {
6868
"assert(foo() == 3); \n"
6969
);
7070
ASSERT_EQUALS("", errout.str());
71+
72+
check(
73+
"int foo(int a) {\n"
74+
" int b=a+1;\n"
75+
" return b;\n"
76+
"}\n"
77+
"assert(foo(1) == 2); \n"
78+
);
79+
ASSERT_EQUALS("", errout.str());
7180
}
7281

7382
void functionCallInAssert() {
@@ -138,4 +147,4 @@ class TestAssert : public TestFixture {
138147
}
139148
};
140149

141-
REGISTER_TEST(TestAssert)
150+
REGISTER_TEST(TestAssert)

0 commit comments

Comments
 (0)