diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fd253a2f1ad..506568d1a3b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7450,9 +7450,9 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co else if (Token::Match(varName, "%name% [")) { tok2 = varName->next(); - while (Token::Match(tok2->link(), "] ,|=|[")) + while (Token::Match(tok2->link(), "] [,=[{]")) tok2 = tok2->link()->next(); - if (!Token::Match(tok2, "=|,")) + if (!Token::Match(tok2, "[=,{]")) tok2 = nullptr; if (tok2 && tok2->str() == "=") { while (tok2 && tok2->str() != "," && tok2->str() != ";") { @@ -7522,9 +7522,11 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co varTok = varTok->next(); if (!varTok) syntaxError(tok2); // invalid code - TokenList::insertTokens(eq, varTok, 2); - eq->str(";"); - eq->isSplittedVarDeclEq(true); + if (eq->str() == "=") { + TokenList::insertTokens(eq, varTok, 2); + eq->str(";"); + eq->isSplittedVarDeclEq(true); + } // "= x, " => "= x; type " if (tok2->str() == ",") { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d698ca5ce1a..b872a152fa0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -295,6 +295,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(simplifyInitVar2); TEST_CASE(simplifyInitVar3); TEST_CASE(simplifyInitVar4); + TEST_CASE(simplifyInitVar5); TEST_CASE(bitfields1); TEST_CASE(bitfields2); @@ -4678,6 +4679,11 @@ class TestTokenizer : public TestFixture { "}", tokenizeAndStringify(code)); } + void simplifyInitVar5() { // #14218 + const char code[] = "int c[1]{}, b;"; + ASSERT_EQUALS("int c [ 1 ] { } ; int b ;", tokenizeAndStringify(code)); + } + void bitfields1() { const char code1[] = "struct A { bool x : 1; };"; ASSERT_EQUALS("struct A { bool x ; } ;", tokenizeAndStringify(code1));