diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 8faa5ef0d..e0b79743d 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -137,6 +137,9 @@ void CheckAutoVariables::autoVariables() bool begin_function_decl = false; int bindent = 0; + // Which variables have an unknown type? + std::set unknown_type; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { @@ -188,17 +191,24 @@ void CheckAutoVariables::autoVariables() else if (Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok)) { addVD(tok->next()->varId()); + if (!tok->isStandardType() && + NULL == Token::findmatch(_tokenizer->tokens(), ("struct|class " + tok->str()).c_str())) + { + unknown_type.insert(tok->next()->varId()); + } } else if (Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok) && isTypeName(tok->next())) { addVD(tok->tokAt(2)->varId()); } + //Critical assignment else if (Token::Match(tok, "[;{}] %var% = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(4))) { errorAutoVariableAssignment(tok); } - else if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5))) + else if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5)) && + unknown_type.find(tok->tokAt(5)->varId()) == unknown_type.end()) { errorAutoVariableAssignment(tok); } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 38bff2d9d..1f6acf903 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -68,6 +68,7 @@ private: TEST_CASE(testautovar_return); TEST_CASE(testautovar_extern); TEST_CASE(testinvaliddealloc); + TEST_CASE(testassign); // Ticket #1819 TEST_CASE(returnLocalVariable1); TEST_CASE(returnLocalVariable2); @@ -149,6 +150,15 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + void testassign() + { + check("void f(EventPtr *eventP, ActionPtr **actionsP) {\n" + " EventPtr event = *eventP;\n" + " *actionsP = &event->actions;\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } + void returnLocalVariable1() { check("char *foo()\n"