Uninitialized variables: Fixed false positive when using ?:
This commit is contained in:
parent
77e09d72a1
commit
71e61fb1ed
|
@ -1649,7 +1649,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
|
|||
}
|
||||
}
|
||||
|
||||
if (Token::Match(vartok->previous(), "++|--|?|:|%cop%")) {
|
||||
if (Token::Match(vartok->previous(), "++|--|%cop%")) {
|
||||
if (cpp && vartok->previous()->str() == ">>") {
|
||||
// assume that variable is initialized
|
||||
return false;
|
||||
|
@ -1688,6 +1688,23 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
|
|||
if (Token::Match(vartok->previous(), "= %var% ;|%cop%"))
|
||||
return true;
|
||||
|
||||
if (Token::Match(vartok->previous(), "? %var%")) {
|
||||
// this is only variable usage if variable is either:
|
||||
// * unconditionally uninitialized
|
||||
// * used in both rhs and lhs of ':' operator
|
||||
bool rhs = false;
|
||||
for (const Token *tok2 = vartok; tok2; tok2 = tok2->next()) {
|
||||
if (tok2->str() == "(")
|
||||
tok2 = tok2->link();
|
||||
else if (tok2->str() == ":")
|
||||
rhs = true;
|
||||
else if (Token::Match(tok2, "[)];,{}]"))
|
||||
break;
|
||||
else if (rhs && tok2->varId() == vartok->varId())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool unknown = false;
|
||||
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
|
||||
// function parameter?
|
||||
|
|
|
@ -2143,6 +2143,13 @@ private:
|
|||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkUninitVar2("int f(int a) {\n"
|
||||
" int x;\n"
|
||||
" if (a==3) { x=2; }\n"
|
||||
" y = (a==3) ? x : a;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// = { .. }
|
||||
checkUninitVar2("int f() {\n"
|
||||
" int a;\n"
|
||||
|
|
Loading…
Reference in New Issue