Fixed #7839 (Prefix increment triggers parameter modification warning)
This commit is contained in:
parent
f5ad7482a8
commit
aaf19c1e4f
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue