From 54de731cacd774b6689b27cd333683a52dcf128e Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 21 Jan 2015 12:20:03 +0100 Subject: [PATCH] Refactorized CheckUninitVar::checkScope(), fixed false negative --- lib/checkuninitvar.cpp | 25 +++++++++++++------------ test/testuninitvar.cpp | 7 +++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ff9ed6bbe..bda40a806 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1060,35 +1060,36 @@ void CheckUninitVar::checkScope(const Scope* scope) if ((_tokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Type::True) || i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference()) continue; + // don't warn for try/catch exception variable - { - const Token *start = i->typeStartToken(); - while (start && start->isName()) - start = start->previous(); - if (start && Token::simpleMatch(start->previous(), "catch (")) - continue; - } + if (i->isThrow()) + continue; + if (i->nameToken()->strAt(1) == "(" || i->nameToken()->strAt(1) == "{") continue; + + if (Token::Match(i->nameToken(), "%var% =")) { // Variable is initialized, but Rhs might be not + checkRhs(i->nameToken(), *i, false, ""); + continue; + } + bool stdtype = _tokenizer->isC(); const Token* tok = i->typeStartToken(); for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) { if (tok->isStandardType()) stdtype = true; } + while (tok && tok->str() != ";") tok = tok->next(); if (!tok) continue; - if (Token::Match(i->nameToken(), "%var% =")) { - checkRhs(i->nameToken(), *i, false, ""); - continue; - } + if (stdtype || i->isPointer()) { bool alloc = false; checkScopeForVariable(scope, tok, *i, nullptr, nullptr, &alloc, ""); } - if (Token::Match(i->typeStartToken(), "struct %type% *| %var% ;")) + if (i->type()) checkStruct(scope, tok, *i); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 1f0cfe8fa..61cb96b50 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2965,6 +2965,13 @@ private: "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str()); + checkUninitVar2("struct AB { int a; int b; };\n" + "void f(void) {\n" + " AB ab;\n" + " int a = ab.a;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str()); + checkUninitVar2("struct AB { int a; int b; };\n" "void f(void) {\n" " struct AB ab;\n"