diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 038385450..c92f47dc6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5612,6 +5612,14 @@ void Tokenizer::simplifyCasts() { for (Token *tok = _tokens; tok; tok = tok->next()) { + // #2897 : don't remove cast in such cases: + // *((char *)a + 1) = 0; + if (!tok->isName() && Token::simpleMatch(tok->next(), "* (")) + { + tok = tok->tokAt(2)->link(); + continue; + } + while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") || Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%") || (!tok->isName() && (Token::Match(tok->next(), "( %type% * ) (") || diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 640e6e707..22446ac3f 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -434,6 +434,9 @@ private: ASSERT_EQUALS("class A { A operator* ( int ) ; } ;", tok("class A { A operator *(int); };")); ASSERT_EQUALS("class A { A operator* ( int ) const ; } ;", tok("class A { A operator *(int) const; };")); ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)(char *)0)")); + + // no simplification as the cast may be important here. see #2897 for example + ASSERT_EQUALS("; * ( ( char * ) p + 1 ) = 0 ;", tok("; *((char *)p + 1) = 0;")); }