Fix false positive for opposite conditions when using different containers (#1143)

* Fix false positive for opposite conditions when using different containers

* Add additional test
This commit is contained in:
Paul Fultz II 2018-04-04 23:43:13 -05:00 committed by Daniel Marjamäki
parent b6504c70ca
commit 0c6f184423
2 changed files with 15 additions and 2 deletions

View File

@ -273,11 +273,15 @@ bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token
if (isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand2(), library, pure))
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
}
if (Library::isContainerYield(cond1, Library::Container::EMPTY, "empty") && Library::isContainerYield(cond2->astOperand1(), Library::Container::SIZE, "size")) {
if (Library::isContainerYield(cond1, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand1(), Library::Container::SIZE, "size") &&
cond1->astOperand1()->astOperand1()->varId() == cond2->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !(cond2->str() == "==" && cond2->astOperand2()->getValue(0));
}
if (Library::isContainerYield(cond2, Library::Container::EMPTY, "empty") && Library::isContainerYield(cond1->astOperand1(), Library::Container::SIZE, "size")) {
if (Library::isContainerYield(cond2, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond1->astOperand1(), Library::Container::SIZE, "size") &&
cond2->astOperand1()->astOperand1()->varId() == cond1->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !(cond1->str() == "==" && cond1->astOperand2()->getValue(0));
}
}

View File

@ -1866,6 +1866,15 @@ private:
check("void f1(const std::string &s, bool b) { if(s.empty() || ((s.size() == 1) && b)) {}} ");
ASSERT_EQUALS("", errout.str());
check("void f1(const std::string &x, const std::string &y) { if(x.size() > 42) if(y.empty()) {}} ");
ASSERT_EQUALS("", errout.str());
check("void f1(const std::string &x, const std::string &y) { if(y.empty()) if(x.size() > 42) {}} ");
ASSERT_EQUALS("", errout.str());
check("void f1(const std::string v[10]) { if(v[0].size() > 42) if(v[1].empty()) {}} ");
ASSERT_EQUALS("", errout.str());
}
void identicalConditionAfterEarlyExit() {