diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 96c69bb4dd9..bc7931d1c70 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8830,17 +8830,19 @@ void Tokenizer::findGarbageCode() const continue; // count number of semicolons int semicolons = 0, colons = 0; - const Token* const startTok = tok; - tok = tok->linkAt(1)->previous(); // find ")" of the for-loop - // walk backwards until we find the beginning (startTok) of the for() again - for (; tok != startTok; tok = tok->previous()) { + const Token* const endTok = tok->linkAt(1); + for (tok = tok->tokAt(2); tok != endTok; tok = tok->next()) { + if (const Token* lam = findLambdaEndTokenWithoutAST(tok)) { + tok = lam; + continue; + } if (tok->str() == ";") { // do the counting semicolons++; } else if (tok->str() == ":") { if (tok->strAt(-1) == ",") syntaxError(tok); colons++; - } else if (tok->str() == ")") { // skip pairs of ( ) + } else if (tok->str() == "(") { // skip pairs of ( ) tok = tok->link(); } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dcbbd0201af..3baa9f4a269 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7683,6 +7683,10 @@ class TestTokenizer : public TestFixture { ASSERT_NO_THROW(tokenizeAndStringify("struct S { unsigned u:2, :30; };")); // #14393 + ASSERT_NO_THROW(tokenizeAndStringify("void f() {\n" // #14395 + " for (int i : [](int a, int b) { ++a; ++b; return std::vector{a, b}; }(1, 2)) {}\n" + "}\n")); + ignore_errout(); }