Avoid constStatement false positives for 'foo() || x=5'. Found in daca@home.

This commit is contained in:
Daniel Marjamäki 2020-09-29 13:35:39 +02:00
parent f956dee58a
commit bf3833dad5
2 changed files with 29 additions and 0 deletions

View File

@ -1665,6 +1665,27 @@ void CheckOther::checkIncompleteStatement()
continue;
if (tok->str() == "," && Token::simpleMatch(tok->astTop()->previous(), "for ("))
continue;
// Do not warn for statement when both lhs and rhs has side effects:
// dostuff() || x=213;
if (Token::Match(tok, "%oror%|&&")) {
bool warn = false;
visitAstNodes(tok, [&warn](const Token *child) {
if (Token::Match(child, "%oror%|&&"))
return ChildrenToVisit::op1_and_op2;
if (child->isAssignmentOp())
return ChildrenToVisit::none;
if (child->tokType() == Token::Type::eIncDecOp)
return ChildrenToVisit::none;
if (Token::Match(child->previous(), "%name% ("))
return ChildrenToVisit::none;
warn = true;
return ChildrenToVisit::done;
});
if (!warn)
continue;
}
const Token *rtok = nextAfterAstRightmostLeaf(tok);
if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") &&
!Token::Match(tok->previous(), ";|}|{ %any% ;"))

View File

@ -90,6 +90,7 @@ private:
TEST_CASE(vardecl);
TEST_CASE(archive); // ar & x
TEST_CASE(ast);
TEST_CASE(oror); // dostuff() || x=32;
}
void test1() {
@ -423,6 +424,13 @@ private:
check("struct c { void a() const { for (int x=0; x;); } };", true);
ASSERT_EQUALS("", errout.str());
}
void oror() {
check("void foo() {\n"
" params_given (params, \"overrides\") || (overrides = \"1\");\n"
"}", true);
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestIncompleteStatement)