diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 45f47bb1d..b4247cb35 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2275,7 +2275,7 @@ bool Tokenizer::simplifyKnownVariables() { incdec(value, tok3->strAt(1)); tok2->tokAt(2)->str(value.c_str()); - if (Token::Match(tok3, "; %any% %any% ;")) + if (Token::Match(tok3, "[;{}] %any% %any% ;")) { tok3->deleteNext(); tok3->deleteNext(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 1ce68068d..12e0781ff 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -68,6 +68,7 @@ private: TEST_CASE(declareVar); TEST_CASE(removePostIncrement); + TEST_CASE(removePreIncrement); TEST_CASE(elseif1); @@ -394,6 +395,19 @@ private: } + void removePreIncrement() + { + const char code[] = "void f()\n" + "{\n" + " unsigned int c = 0;\n" + " ++c;\n" + " if (c>0) { ++c; }\n" + " ++c;\n" + "}\n"; + ASSERT_EQUALS(std::string("void f ( ) { int c ; c = 3 ; ; { ; } ; } "), tok(code)); + } + + std::string elseif(const char code[]) { std::istringstream istr(code);