diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 313f13847..f78b849bf 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -178,15 +178,19 @@ 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% =|++|--") && - isNonReferenceArg(tok->next()) && - !Token::Match(tok->tokAt(2), "= %varid% ;", tok->next()->varId()) && - !variableIsUsedInScope(Token::findsimplematch(tok->tokAt(2), ";"), tok->next()->varId(), scope) && - !Token::findsimplematch(tok, "goto", scope->classEnd)) { - if (tok->next()->variable()->isPointer() && printWarning) - errorUselessAssignmentPtrArg(tok->next()); - else if (printStyle) - errorUselessAssignmentArg(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); + } } } } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 6e862e5a1..2fb043e68 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -328,6 +328,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(int* ptr) {\n" // #3177 + " --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()); } void testautovar11() { // #4641 - fp, assign local struct member address to function parameter