Fixed #4747 (Can not find uninitialized variable (variable used in rhs in assignment))

This commit is contained in:
Daniel Marjamäki 2013-06-27 16:53:15 +02:00
parent da9e3b4f01
commit 6b01d2d5a8
2 changed files with 34 additions and 2 deletions

View File

@ -1395,8 +1395,17 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
// variable is seen..
if (tok->varId() == var.varId()) {
if (!membervar.empty()) {
if (isMemberVariableAssignment(tok, membervar))
if (isMemberVariableAssignment(tok, membervar)) {
bool rhs = false;
for (const Token *tok2 = tok; tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->str() == "=")
rhs = true;
else if (rhs && tok2->varId() == var.varId() && isMemberVariableUsage(tok2, var.isPointer(), membervar))
uninitStructMemberError(tok2, tok2->str() + "." + membervar);
}
return true;
}
if (isMemberVariableUsage(tok, var.isPointer(), membervar))
uninitStructMemberError(tok, tok->str() + "." + membervar);
@ -1409,9 +1418,19 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
if (!suppressErrors && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
uninitvarError(tok, tok->str());
else
else {
if (tok->strAt(1) == "=") {
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId() == tok->varId() && isVariableUsage(tok2, var.isPointer(), _tokenizer->isCPP())) {
uninitvarError(tok, tok->str());
break;
}
}
}
// assume that variable is assigned
return true;
}
}
}
}

View File

@ -1999,6 +1999,12 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("void f() {\n"
" int x;\n"
" x = 3 + 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"
@ -2652,6 +2658,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"
" struct AB ab;\n"
" ab.a = ab.a + 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n"
"void do_something(const struct AB ab);\n"
"void f(void) {\n"