RedundantAssignment: Don't warn for initialization with {0}

This commit is contained in:
Daniel Marjamäki 2019-08-23 08:51:16 +02:00
parent d122b1c722
commit 7061cc334b
2 changed files with 11 additions and 3 deletions

View File

@ -437,10 +437,12 @@ void CheckOther::checkRedundantAssignment()
bool trivial = true;
visitAstNodes(tok->astOperand2(),
[&](const Token *rhs) {
if (Token::Match(rhs, "%str%|%num%|%name%"))
return ChildrenToVisit::op1_and_op2;
if (rhs->str() == "(" && !rhs->previous()->isName())
if (Token::Match(rhs, "{ 0 }"))
return ChildrenToVisit::none;
if (Token::Match(rhs, "0|NULL|nullptr"))
return ChildrenToVisit::op1_and_op2;
if (rhs->isCast())
return ChildrenToVisit::op2;
trivial = false;
return ChildrenToVisit::done;
});

View File

@ -6402,6 +6402,12 @@ private:
" ab.x = 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Variable 'ab.x' is reassigned a value before the old one has been used.\n", errout.str());
check("void f() {\n"
" struct AB ab = {0};\n"
" ab = foo();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void redundantVarAssignment_7133() {