diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 118793d4b..76ed3ba9f 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1118,6 +1118,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int if (tok->previous()->str() == "return") uninitvarError(tok, tok->str()); + else if (Token::Match(tok->next(), "++|--|%op%")) + uninitvarError(tok, tok->str()); + else // assume that variable is assigned return true; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index f0d7940bf..138456d00 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1700,6 +1700,20 @@ private: } void uninitvar2() { + // using uninit var + checkUninitVar2("void f() {\n" + " int x;\n" + " x++;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); + + checkUninitVar2("void f() {\n" + " int x;\n" + " int y = x & 3;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); + + // conditional initialization checkUninitVar2("void f() {\n" " int x;\n" " if (y == 1) { x = 1; }\n"