diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0862ac03b..2be550f5f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4386,7 +4386,7 @@ void Tokenizer::simplifyCompoundAssignment() } // backup current token.. - const Token * const tok1 = tok; + Token * const tok1 = tok; if (tok->strAt(1) == "*") tok = tok->next(); @@ -4424,18 +4424,28 @@ void Tokenizer::simplifyCompoundAssignment() else continue; - // modify the token list.. - tok->str("="); - tok->insertToken(op); - Token *tokend = 0; - for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) + // Remove the whole statement if it says: "+=0;", "-=0;", "*=1;" or "/=1;" + if (Token::Match(tok, "+=|-= 0 ;") || Token::simpleMatch(tok, "|= 0 ;") || Token::Match(tok, "*=|/= 1 ;")) { - tok->insertToken(tok2->str()); - tok->next()->varId(tok2->varId()); - if (Token::simpleMatch(tok->next(), "]")) - tokend = tok->next(); - else if (Token::simpleMatch(tok->next(), "[")) - Token::createMutualLinks(tok->next(), tokend); + tok = tok1; + while (tok->next()->str() != ";") + tok->deleteNext(); + } + else + { + // simplify the compound assignment.. + tok->str("="); + tok->insertToken(op); + Token *tokend = 0; + for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) + { + tok->insertToken(tok2->str()); + tok->next()->varId(tok2->varId()); + if (Token::simpleMatch(tok->next(), "]")) + tokend = tok->next(); + else if (Token::simpleMatch(tok->next(), "[")) + Token::createMutualLinks(tok->next(), tokend); + } } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index db96ea5aa..954cc789f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4648,6 +4648,12 @@ private: 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;")); + + ASSERT_EQUALS(";", tokenizeAndStringify(";x += 0;")); + ASSERT_EQUALS(";", tokenizeAndStringify(";x -= 0;")); + ASSERT_EQUALS(";", tokenizeAndStringify(";x |= 0;")); + ASSERT_EQUALS(";", tokenizeAndStringify(";x *= 1;")); + ASSERT_EQUALS(";", tokenizeAndStringify(";x /= 1;")); } void simplifyAssignmentInFunctionCall()