diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 45ddb8183..71e861afb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4390,11 +4390,24 @@ void Tokenizer::simplifyCompoundAssignment() // variable.. tok = tok->tokAt(2); - while (Token::Match(tok, ". %var%")) - tok = tok->tokAt(2); - while (Token::Match(tok, "[ %any% ]")) - tok = tok->tokAt(3); - + while (Token::Match(tok, ". %var%") || (tok && tok->str() == "[")) + { + if (tok->str() != "[") + tok = tok->tokAt(2); + else + { + // goto "]" + tok = tok->next(); + while (tok && !Token::Match(tok, "++|--|(|[|]")) + tok = tok->next(); + if (!tok) + break; + else if (tok->str() == "]") + tok = tok->next(); + else + break; + } + } if (!tok) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 480527b02..dd7e06c62 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4645,6 +4645,7 @@ private: ASSERT_EQUALS("; x . y = x . y + 1 ;", tokenizeAndStringify("; x.y += 1;")); ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;")); + ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tokenizeAndStringify("; x[y-1] += 1;")); } void simplifyAssignmentInFunctionCall()