Skip to content

Commit d44e45c

Browse files
Fix #14047 FP useStlAlgorithm with continue in loop (#7717)
1 parent 6c11344 commit d44e45c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/checkstl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,8 +2579,8 @@ static bool isEarlyExit(const Token *start)
25792579
if (start->str() != "{")
25802580
return false;
25812581
const Token *endToken = start->link();
2582-
const Token *tok = Token::findmatch(start, "return|throw|break", endToken);
2583-
if (!tok)
2582+
const Token *tok = Token::findmatch(start, "return|throw|break|continue", endToken);
2583+
if (!tok || tok->scope() != start->scope() || tok->str() == "continue")
25842584
return false;
25852585
const Token *endStatement = Token::findsimplematch(tok, "; }", endToken);
25862586
if (!endStatement)

test/teststl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5873,6 +5873,28 @@ class TestStl : public TestFixture {
58735873
" return b;\n"
58745874
"}\n");
58755875
ASSERT_EQUALS("", errout_str());
5876+
5877+
check("bool get_index(const std::vector<int>& items, int value, size_t& index) {\n" // #14047
5878+
" size_t current_index = 0;\n"
5879+
" for (size_t i = 0; i < items.size(); ++i) {\n"
5880+
" if (items[i] == value) {\n"
5881+
" if (current_index != index) {\n"
5882+
" current_index++;\n"
5883+
" continue;\n"
5884+
" }\n"
5885+
" index = i;\n"
5886+
" return true;\n"
5887+
" }\n"
5888+
" }\n"
5889+
" return false;\n"
5890+
"}\n"
5891+
"int main() {\n"
5892+
" std::vector<int> items{ 0, 1, 1, 1 };\n"
5893+
" size_t index = 2;\n"
5894+
" get_index(items, 1, index);\n"
5895+
" return index;\n"
5896+
"}\n");
5897+
ASSERT_EQUALS("", errout_str());
58765898
}
58775899

58785900
void loopAlgoMinMax() {

0 commit comments

Comments
 (0)