diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index c5eef3e47..d0f9703d9 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1685,6 +1685,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp } } + if (Token::Match(vartok->previous(), "= %var% ;|%cop%")) + return true; + bool unknown = false; if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) { // function parameter? diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 2f1815559..51e17f6fa 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2081,6 +2081,12 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); + checkUninitVar2("int f() {\n" + " int x;\n" + " x = x;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); + checkUninitVar2("void f() {\n" " struct ABC *abc;\n" " abc->a = 0;\n"