Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1810,21 +1810,32 @@
return commutativeEquals;
}

static bool isZeroBoundCond(const Token * const cond)
static bool isZeroBoundCond(const Token * const cond, bool reverse)
{
if (cond == nullptr)
return false;
// Assume unsigned
// TODO: Handle reverse conditions
const bool isZero = cond->astOperand2()->getValue(0);
if (cond->str() == "==" || cond->str() == ">=")
return isZero;
if (cond->str() == "<=")
return true;
if (cond->str() == "<")
return !isZero;
if (cond->str() == ">")
return false;
const bool isZero = reverse ? cond->astOperand1()->getValue(0) : cond->astOperand2()->getValue(0);

Check warning

Code scanning / Cppcheck Premium

A cast should not convert a pointer type to an integral type Warning

A cast should not convert a pointer type to an integral type
if (reverse) {
if (cond->str() == "==" || cond->str() == "<=")
return isZero;
if (cond->str() == ">=")
return true;
if (cond->str() == ">")
return !isZero;
if (cond->str() == "<")
return false;
} else {
if (cond->str() == "==" || cond->str() == ">=")
return isZero;
if (cond->str() == "<=")
return true;
if (cond->str() == "<")
return !isZero;
if (cond->str() == ">")
return false;
}

return false;
}

Expand Down Expand Up @@ -1888,7 +1899,7 @@
if (isSameExpression(true, cond1->astOperand2(), cond2->astOperand2(), settings, pure, followVar, errors))
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
}
// TODO: Handle reverse conditions

if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand1(), Library::Container::Yield::SIZE, "size") &&
isSameExpression(true,
Expand All @@ -1898,7 +1909,19 @@
pure,
followVar,
errors)) {
return !isZeroBoundCond(cond2);
return !isZeroBoundCond(cond2, false);
}

if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand2(), Library::Container::Yield::SIZE, "size") &&
isSameExpression(true,
cond1->astOperand1()->astOperand1(),
cond2->astOperand2()->astOperand1()->astOperand1(),
settings,
pure,
followVar,
errors)) {
return !isZeroBoundCond(cond2, true);
}

if (Library::isContainerYield(cond2, Library::Container::Yield::EMPTY, "empty") &&
Expand All @@ -1910,7 +1933,19 @@
pure,
followVar,
errors)) {
return !isZeroBoundCond(cond1);
return !isZeroBoundCond(cond1, false);
}

if (Library::isContainerYield(cond2, Library::Container::Yield::EMPTY, "empty") &&
Library::isContainerYield(cond1->astOperand2(), Library::Container::Yield::SIZE, "size") &&
isSameExpression(true,
cond2->astOperand1()->astOperand1(),
cond1->astOperand2()->astOperand1()->astOperand1(),
settings,
pure,
followVar,
errors)) {
return !isZeroBoundCond(cond1, true);
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,12 @@ class TestCondition : public TestFixture {
check("void f1(const std::string &s) { if(s.size() >= 0) if(s.empty()) {}} "); // CheckOther says: Unsigned expression 's.size()' can't be negative so it is unnecessary to test it. [unsignedPositive]
ASSERT_EQUALS("", errout_str());

check("void f1(const std::string &s) { if(42 < s.size()) if(s.empty()) {}}");
ASSERT_EQUALS("[test.cpp:1:39] -> [test.cpp:1:61]: (warning) Opposite inner 'if' condition leads to a dead code block. [oppositeInnerCondition]\n", errout_str());

check("void f1(const std::string &s) { if(s.empty()) if(42 < s.size()) {}}");
ASSERT_EQUALS("[test.cpp:1:43] -> [test.cpp:1:53]: (warning) Opposite inner 'if' condition leads to a dead code block. [oppositeInnerCondition]\n", errout_str());

// TODO: These are identical condition since size cannot be negative
check("void f1(const std::string &s) { if(s.size() <= 0) if(s.empty()) {}}");
ASSERT_EQUALS("", errout_str());
Expand Down
Loading