Uninitialized variables; Fixed false positive for overloaded & 'ar & a & b & c'
This commit is contained in:
parent
216918756b
commit
e35c46bcb9
|
@ -1166,8 +1166,19 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
|
|||
if (astIsRhs(derefValue) && isLikelyStreamRead(mTokenizer->isCPP(), derefValue->astParent()))
|
||||
return nullptr;
|
||||
|
||||
if (mTokenizer->isCPP() && Token::simpleMatch(valueExpr->astParent(), "&") && !valueExpr->astParent()->astParent() && astIsRhs(valueExpr) && Token::Match(valueExpr->astSibling(), "%type%"))
|
||||
return nullptr;
|
||||
// Assignment with overloaded &
|
||||
if (mTokenizer->isCPP() && Token::simpleMatch(valueExpr->astParent(), "&") && astIsRhs(valueExpr)) {
|
||||
const Token *parent = valueExpr->astParent();
|
||||
while (Token::simpleMatch(parent, "&") && parent->isBinaryOp())
|
||||
parent = parent->astParent();
|
||||
if (!parent) {
|
||||
const Token *lhs = valueExpr->astParent();
|
||||
while (Token::simpleMatch(lhs, "&") && lhs->isBinaryOp())
|
||||
lhs = lhs->astOperand1();
|
||||
if (lhs && lhs->isName() && (!lhs->valueType() || lhs->valueType()->type <= ValueType::Type::CONTAINER))
|
||||
return nullptr; // <- possible assignment
|
||||
}
|
||||
}
|
||||
|
||||
return derefValue ? derefValue : valueExpr;
|
||||
}
|
||||
|
|
|
@ -534,12 +534,19 @@ private:
|
|||
" a & x;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkUninitVar("void f(int a) {\n"
|
||||
" int x;\n"
|
||||
" a & x;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
||||
|
||||
checkUninitVar("void f() {\n"
|
||||
" int a,b,c;\n"
|
||||
" ar & a & b & c;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkUninitVar("void a() {\n" // asm
|
||||
" int x;\n"
|
||||
" asm();\n"
|
||||
|
|
Loading…
Reference in New Issue