diff --git a/lib/checkboost.cpp b/lib/checkboost.cpp index fc1130ba9..779cd87e4 100644 --- a/lib/checkboost.cpp +++ b/lib/checkboost.cpp @@ -46,7 +46,9 @@ void CheckBoost::checkBoostForeachModification() const Token *end = tok2->link(); for (; tok2 != end; tok2 = tok2->next()) { if (Token::Match(tok2, "%varid% . insert|erase|push_back|push_front|pop_front|pop_back|clear|swap|resize|assign|merge|remove|remove_if|reverse|sort|splice|unique|pop|push", container_id)) { - boostForeachError(tok2); + const Token* nextStatement = Token::findsimplematch(tok2->linkAt(3), ";", end); + if (!Token::Match(nextStatement, "; break|return|throw")) + boostForeachError(tok2); break; } } diff --git a/test/testboost.cpp b/test/testboost.cpp index 5dba23203..f869d12a7 100644 --- a/test/testboost.cpp +++ b/test/testboost.cpp @@ -97,6 +97,16 @@ private: " data.insert(i);\n" "}"); ASSERT_EQUALS("", errout.str()); + + // Break after modification (#4788) + check("void f() {\n" + " vector data;\n" + " BOOST_FOREACH(int i, data) {\n" + " data.push_back(123);\n" + " break;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } };