From b97387db667498aca6cb284ca40ddf1346cba25b Mon Sep 17 00:00:00 2001 From: abhijit-sawant Date: Mon, 18 Jan 2021 02:11:37 -0500 Subject: [PATCH] Made missing comparison in loop check more generic (#3048) --- lib/checkstl.cpp | 13 ++++--------- test/teststl.cpp | 5 ++++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index ac51a00cd..f4bc4f8f2 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1732,8 +1732,6 @@ void CheckStl::missingComparison() } const Token *incrementToken = nullptr; - bool bComparedInAdvance = false; - // Parse loop.. for (const Token *tok3 = scope.bodyStart; tok3 != scope.bodyEnd; tok3 = tok3->next()) { if (tok3->varId() == iteratorId) { @@ -1742,16 +1740,13 @@ void CheckStl::missingComparison() tok3 = tok3->linkAt(6); if (!tok3) break; - } else if (Token::simpleMatch(tok3->astParent(), "++")) { - if (!bComparedInAdvance) + } else if (Token::simpleMatch(tok3->astParent(), "++")) incrementToken = tok3; - else - bComparedInAdvance = false; - } else if (Token::simpleMatch(tok3->astParent(), "+")) { - if (Token::simpleMatch(tok3->astSibling(), "1")) { + else if (Token::simpleMatch(tok3->astParent(), "+")) { + if (Token::Match(tok3->astSibling(), "%num%")) { const Token* tokenGrandParent = tok3->astParent()->astParent(); if (Token::Match(tokenGrandParent, "==|!=")) - bComparedInAdvance = true; + break; } } else if (Token::Match(tok3->astParent(), "==|!=")) incrementToken = nullptr; diff --git a/test/teststl.cpp b/test/teststl.cpp index 68076334e..a440fa44e 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -3004,8 +3004,11 @@ private: check("void f(const std::vector &v) {\n" " for(std::vector::const_iterator it = v.begin(); it != v.end(); ++it) {\n" - " if(it+1 != v.end())\n" + " if(it+2 != v.end())\n" + " {\n" " ++it;\n" + " ++it;\n" + " }\n" " }\n" "}"); ASSERT_EQUALS("", errout.str());