From bf3833dad5983797e5747ef36ab06eec5a4bbc49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Sep 2020 13:35:39 +0200 Subject: [PATCH] Avoid constStatement false positives for 'foo() || x=5'. Found in daca@home. --- lib/checkother.cpp | 21 +++++++++++++++++++++ test/testincompletestatement.cpp | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 810ef0f9d..46d4d57ce 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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% ;")) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 57d4c49a0..4495d0972 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -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)