diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 36efe94c6..2926c4356 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1335,24 +1335,27 @@ private: const Token *parse(const Token &tok, bool &foundError, std::list &checks) const { // Variable declaration.. - if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;")) + if (tok.str() != "return") { - const Token * vartok = tok.next(); - const bool p(vartok->str() == "*"); - if (p) - vartok = vartok->next(); - if ((p || tok.isStandardType()) && vartok->varId() != 0) - checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, false)); - return vartok->next(); - } + if (Token::Match(tok.previous(), "[;{}] %type% *| %var% ;")) + { + const Token * vartok = tok.next(); + const bool p(vartok->str() == "*"); + if (p) + vartok = vartok->next(); + if ((p || tok.isStandardType()) && vartok->varId() != 0) + checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), p, false)); + return vartok->next(); + } - // Variable declaration for array.. - if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;")) - { - const Token * vartok = tok.next(); - if (vartok->varId() != 0) - checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), false, true)); - return vartok->next()->link()->next(); + // Variable declaration for array.. + if (Token::Match(tok.previous(), "[;{}] %type% %var% [ %num% ] ;")) + { + const Token * vartok = tok.next(); + if (vartok->varId() != 0) + checks.push_back(new CheckUninitVar(owner, vartok->varId(), vartok->str(), false, true)); + return vartok->next()->link()->next(); + } } if (tok.varId()) diff --git a/test/testother.cpp b/test/testother.cpp index 1ee858fad..9a81b9a7e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -991,6 +991,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str()); + checkUninitVar("static int foo()\n" + "{\n" + " int ret;\n" + " return ret;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str()); + checkUninitVar("static void foo()\n" "{\n" " int x, y;\n"