Fixed #7839 (Prefix increment triggers parameter modification warning)

This commit is contained in:
Daniel Marjamäki 2016-12-19 15:25:36 +01:00
parent f5ad7482a8
commit aaf19c1e4f
2 changed files with 19 additions and 13 deletions

View File

@ -178,19 +178,20 @@ void CheckAutoVariables::assignFunctionArg()
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% =|++|--") || Token::Match(tok, "[;{}] ++|-- %var%")) {
const Token* vartok = tok->next();
if (!vartok->variable())
vartok = vartok->next();
if (isNonReferenceArg(vartok) &&
!Token::Match(vartok->next(), "= %varid% ;", vartok->varId()) &&
!variableIsUsedInScope(Token::findsimplematch(vartok->next(), ";"), vartok->varId(), scope) &&
!Token::findsimplematch(vartok, "goto", scope->classEnd)) {
if (vartok->variable()->isPointer() && printWarning)
errorUselessAssignmentPtrArg(vartok);
else if (printStyle)
errorUselessAssignmentArg(vartok);
}
// TODO: What happens if this is removed?
if (tok->astParent())
continue;
if (!Token::Match(tok, "=|++|--") || !Token::Match(tok->astOperand1(), "%var%"))
continue;
const Token* const vartok = tok->astOperand1();
if (isNonReferenceArg(vartok) &&
!Token::Match(vartok->next(), "= %varid% ;", vartok->varId()) &&
!variableIsUsedInScope(Token::findsimplematch(vartok->next(), ";"), vartok->varId(), scope) &&
!Token::findsimplematch(vartok, "goto", scope->classEnd)) {
if (vartok->variable()->isPointer() && printWarning)
errorUselessAssignmentPtrArg(vartok);
else if (printStyle)
errorUselessAssignmentArg(vartok);
}
}
}

View File

@ -333,6 +333,11 @@ private:
" --ptr;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?\n", errout.str());
check("void foo(struct S* const x) {\n" // #7839
" ++x->n;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testautovar11() { // #4641 - fp, assign local struct member address to function parameter