From d342387b952d690dbaa0938212ea814e49970005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Nov 2013 18:07:33 +0100 Subject: [PATCH] Uninitialized var: Fixed false negatives for self assignment --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 6 ++++++ 2 files changed, 9 insertions(+) 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"