From ba463295c28b2fa0015525ac277ed53edb32228c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 14 Dec 2011 06:00:17 +0100 Subject: [PATCH] Uninitialized variables: improved check to detect more errors. Ticket: #3369 --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) 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"