Skip to content

Commit a4a2252

Browse files
Fix #9938 (false positive: StlMissingComparison) (#2989)
1 parent da198e0 commit a4a2252

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

lib/checkstl.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,23 +1637,41 @@ void CheckStl::missingComparison()
16371637
}
16381638

16391639
const Token *incrementToken = nullptr;
1640+
bool bComparedInAdvance = false;
16401641

16411642
// Parse loop..
1642-
for (const Token *tok3 = scope.bodyStart; tok3 != scope.bodyEnd; tok3 = tok3->next()) {
1643-
if (Token::Match(tok3, "%varid% ++", iteratorId))
1644-
incrementToken = tok3;
1645-
else if (Token::Match(tok3->previous(), "++ %varid% !!.", iteratorId))
1646-
incrementToken = tok3;
1647-
else if (Token::Match(tok3, "%varid% !=|==", iteratorId))
1648-
incrementToken = nullptr;
1643+
for (const Token *tok3 = scope.bodyStart; tok3 != scope.bodyEnd; tok3 = tok3->next())
1644+
{
1645+
if (tok3->varId() == iteratorId)
1646+
{
1647+
if (Token::Match(tok3, "%varid% = %name% . insert ( ++| %varid% ++| ,", iteratorId))
1648+
{
1649+
// skip insertion..
1650+
tok3 = tok3->linkAt(6);
1651+
if (!tok3)
1652+
break;
1653+
}
1654+
else if (Token::simpleMatch(tok3->astParent(), "++"))
1655+
{
1656+
if (!bComparedInAdvance)
1657+
incrementToken = tok3;
1658+
else
1659+
bComparedInAdvance = false;
1660+
}
1661+
else if (Token::simpleMatch(tok3->astParent(), "+"))
1662+
{
1663+
if (Token::simpleMatch(tok3->astSibling(), "1"))
1664+
{
1665+
const Token* tokenGrandParent = tok3->astParent()->astParent();
1666+
if (Token::Match(tokenGrandParent, "==|!="))
1667+
bComparedInAdvance = true;
1668+
}
1669+
}
1670+
else if (Token::Match(tok3->astParent(), "==|!="))
1671+
incrementToken = nullptr;
1672+
}
16491673
else if (tok3->str() == "break" || tok3->str() == "return")
16501674
incrementToken = nullptr;
1651-
else if (Token::Match(tok3, "%varid% = %name% . insert ( ++| %varid% ++| ,", iteratorId)) {
1652-
// skip insertion..
1653-
tok3 = tok3->linkAt(6);
1654-
if (!tok3)
1655-
break;
1656-
}
16571675
}
16581676
if (incrementToken)
16591677
missingComparisonError(incrementToken, tok2->tokAt(16));

test/teststl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,6 +2986,14 @@ class TestStl : public TestFixture {
29862986
" }\n"
29872987
"}");
29882988
ASSERT_EQUALS("", errout.str());
2989+
2990+
check("void f(const std::vector<std::string> &v) {\n"
2991+
" for(std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it) {\n"
2992+
" if(it+1 != v.end())\n"
2993+
" ++it;\n"
2994+
" }\n"
2995+
"}");
2996+
ASSERT_EQUALS("", errout.str());
29892997
}
29902998

29912999
void missingInnerComparison2() {

0 commit comments

Comments
 (0)