Fixed #7130 (Wrong assignmentInAssert when using lambda in predicate function)
This commit is contained in:
parent
54be403f64
commit
2a42f245a9
|
@ -44,7 +44,7 @@ void CheckAssert::assertWithSideEffects()
|
|||
|
||||
const Token *endTok = tok->next()->link();
|
||||
for (const Token* tmp = tok->next(); tmp != endTok; tmp = tmp->next()) {
|
||||
checkVariableAssignment(tmp);
|
||||
checkVariableAssignment(tmp, tok->scope());
|
||||
|
||||
if (tmp->tokType() == Token::eFunction) {
|
||||
const Function* f = tmp->function();
|
||||
|
@ -108,12 +108,21 @@ void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& v
|
|||
}
|
||||
|
||||
// checks if side effects happen on the variable prior to tmp
|
||||
void CheckAssert::checkVariableAssignment(const Token* assignTok)
|
||||
void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
|
||||
{
|
||||
const Variable* prevVar = assignTok->previous()->variable();
|
||||
if (!prevVar)
|
||||
return;
|
||||
|
||||
// Variable declared in inner scope in assert => dont warn
|
||||
if (assertionScope != prevVar->scope()) {
|
||||
const Scope *s = prevVar->scope();
|
||||
while (s && s != assertionScope)
|
||||
s = s->nestedIn;
|
||||
if (s == assertionScope)
|
||||
return;
|
||||
}
|
||||
|
||||
// assignment
|
||||
if (assignTok->isAssignmentOp() || assignTok->tokType() == Token::eIncDecOp) {
|
||||
if (prevVar->isConst())
|
||||
|
|
|
@ -49,7 +49,7 @@ public:
|
|||
void assertWithSideEffects();
|
||||
|
||||
protected:
|
||||
void checkVariableAssignment(const Token* tmp);
|
||||
void checkVariableAssignment(const Token* tmp, const Scope *assertionScope);
|
||||
static bool inSameScope(const Token* returnTok, const Token* assignTok);
|
||||
|
||||
private:
|
||||
|
|
|
@ -217,6 +217,15 @@ private:
|
|||
" return a;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" assert(std::all_of(first, last, []() {\n"
|
||||
" auto tmp = x.someValue();\n"
|
||||
" auto const expected = someOtherValue;\n"
|
||||
" return tmp == expected;\n"
|
||||
" }));\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue