Skip to content

Commit 986ec42

Browse files
amai2012danmar
authored andcommitted
Fixed danmar#4937 (false positive: Assert calls a function which may have desired side effects)
1 parent 6e536b9 commit 986ec42

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

lib/checkassert.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ void CheckAssert::assertWithSideEffects()
6969
if (tok2->type() != Token::eAssignmentOp && tok2->type() != Token::eIncDecOp) continue;
7070
const Variable* var = tok2->previous()->variable();
7171
if (!var || var->isLocal()) continue;
72-
72+
if (var->isArgument() &&
73+
(var->isConst() || (!var->isReference() && !var->isPointer())))
74+
// see ticket #4937. Assigning function arguments not passed by reference is ok.
75+
continue;
7376
std::vector<const Token*> returnTokens; // find all return statements
7477
for (const Token *rt = scope->classStart; rt != scope->classEnd; rt = rt->next()) {
7578
if (!Token::Match(rt, "return %any%")) continue;

lib/symboldatabase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
16101610
std::cout << indent << " isMutable: " << (var->isMutable() ? "true" : "false") << std::endl;
16111611
std::cout << indent << " isStatic: " << (var->isStatic() ? "true" : "false") << std::endl;
16121612
std::cout << indent << " isExtern: " << (var->isExtern() ? "true" : "false") << std::endl;
1613+
std::cout << indent << " isLocal: " << (var->isLocal() ? "true" : "false") << std::endl;
16131614
std::cout << indent << " isConst: " << (var->isConst() ? "true" : "false") << std::endl;
16141615
std::cout << indent << " isClass: " << (var->isClass() ? "true" : "false") << std::endl;
16151616
std::cout << indent << " isArray: " << (var->isArray() ? "true" : "false") << std::endl;

test/testassert.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,51 @@ class TestAssert : public TestFixture {
8989
"assert(foo() == 3); \n"
9090
);
9191
ASSERT_EQUALS("[test.cpp:6]: (warning) Assert statement calls a function which may have desired side effects: 'foo'.\n", errout.str());
92+
93+
// Ticket #4937 "false positive: Assert calls a function which may have desired side effects"
94+
check("struct SquarePack {\n"
95+
" static bool isRank1Or8( Square sq ) {\n"
96+
" sq &= 0x38;\n"
97+
" return sq == 0 || sq == 0x38;\n"
98+
" }\n"
99+
"};\n"
100+
"void foo() {\n"
101+
" assert( !SquarePack::isRank1Or8(push2) );\n"
102+
"}\n");
103+
ASSERT_EQUALS("", errout.str());
104+
105+
check("struct SquarePack {\n"
106+
" static bool isRank1Or8( Square &sq ) {\n"
107+
" sq &= 0x38;\n"
108+
" return sq == 0 || sq == 0x38;\n"
109+
" }\n"
110+
"};\n"
111+
"void foo() {\n"
112+
" assert( !SquarePack::isRank1Or8(push2) );\n"
113+
"}\n");
114+
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
115+
116+
check("struct SquarePack {\n"
117+
" static bool isRank1Or8( Square *sq ) {\n"
118+
" *sq &= 0x38;\n"
119+
" return *sq == 0 || *sq == 0x38;\n"
120+
" }\n"
121+
"};\n"
122+
"void foo() {\n"
123+
" assert( !SquarePack::isRank1Or8(push2) );\n"
124+
"}\n");
125+
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
126+
127+
check("struct SquarePack {\n"
128+
" static bool isRank1Or8( Square *sq ) {\n"
129+
" sq &= 0x38;\n"
130+
" return sq == 0 || sq == 0x38;\n"
131+
" }\n"
132+
"};\n"
133+
"void foo() {\n"
134+
" assert( !SquarePack::isRank1Or8(push2) );\n"
135+
"}\n");
136+
TODO_ASSERT_EQUALS("", "[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
92137
}
93138

94139
void assignmentInAssert() {

0 commit comments

Comments
 (0)