diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b46910095..18fbf69fd 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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; }); diff --git a/test/testother.cpp b/test/testother.cpp index 107f4f5cb..d55c99c37 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {