diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 78eac509f..870430060 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2437,23 +2437,24 @@ void Tokenizer::simplifyCasts() { for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok->next(), "( %type% *| )") || Token::Match(tok->next(), "( %type% %type% *| )")) + while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") || + Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%")) { if (tok->isName() && tok->str() != "return") - continue; - - // Is it a cast of some variable? - const Token *tok2 = tok->tokAt(3); - while (tok2 && tok2->str() != ")") - tok2 = tok2->next(); - if (!Token::Match(tok2, ") %var%")) - continue; + break; // Remove cast.. Token::eraseTokens(tok, tok->next()->link()->next()); + + if (tok->str() == ")" && tok->link()->previous()) + { + // If there was another cast before this, go back + // there to check it also. e.g. "(int)(char)x" + tok = tok->link()->previous(); + } } - else if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <")) + if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <")) { Token *tok2 = tok->next(); unsigned int level = 0; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index ee6be8f62..d0bbe8140 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -165,6 +165,10 @@ private: ASSERT_EQUALS(expected, tok(code)); } + + ASSERT_EQUALS("if ( * a )", tok("if ((char)*a)")); + ASSERT_EQUALS("if ( & a )", tok("if ((int)&a)")); + ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)")); }