diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a13b05cef62..6018d264376 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7597,7 +7597,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co if (only_k_r_fpar && finishedwithkr) { if (Token::Match(tok, "(|[|{")) { tok = tok->link(); - if (tok->next() && Token::Match(tok, ") !!{")) + // Don't step onto tokEnd: the loop increment would then step past it + if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{")) tok = tok->next(); else continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d527b98f62c..ec3bdda6e00 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -241,6 +241,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl34); TEST_CASE(vardecl35); TEST_CASE(vardecl36); + TEST_CASE(vardecl38); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -3081,6 +3082,11 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } + void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}' + ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;", + tokenizeAndStringify("int* q{new int[2]{(1)}};")); + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n"