diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index f13d204c5..1d1e16335 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -840,7 +840,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) // keep parentheses here: Functor()(a ... ) // keep parentheses here: ) ( var ) ; if ((Token::Match(tok->next(), "( %var% ) ;|)|,|]") || - (Token::Match(tok->next(), "( %var% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&]")))) && + (Token::Match(tok->next(), "( %var% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&+-]")))) && !tok->isName() && tok->str() != ">" && tok->str() != "]" && diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index c14161fec..e4c3e88aa 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -94,6 +94,7 @@ private: TEST_CASE(removeCast10); TEST_CASE(removeCast11); TEST_CASE(removeCast12); + TEST_CASE(removeCast13); TEST_CASE(inlineasm); @@ -1051,6 +1052,27 @@ private: ASSERT_EQUALS("; ( ( short * ) data ) [ 5 ] = 0 ;", tokenizeAndStringify("; ((short*)data)[5] = 0;", true)); } + void removeCast13() { + // casting deref / address of + ASSERT_EQUALS("; int x ; x = * y ;", tokenizeAndStringify(";int x=(int)*y;",true)); + ASSERT_EQUALS("; int x ; x = & y ;", tokenizeAndStringify(";int x=(int)&y;",true)); + TODO_ASSERT_EQUALS("; int x ; x = ( INT ) * y ;", + "; int x ; x = * y ;", + tokenizeAndStringify(";int x=(INT)*y;",true)); // INT might be a variable + TODO_ASSERT_EQUALS("; int x ; x = ( INT ) & y ;", + "; int x ; x = & y ;", + tokenizeAndStringify(";int x=(INT)&y;",true)); // INT might be a variable + + // #4899 - False positive on unused variable + ASSERT_EQUALS("; float angle ; angle = tilt ;", tokenizeAndStringify("; float angle = (float) tilt;", true)); // status quo + TODO_ASSERT_EQUALS("; float angle ; angle = - tilt ;", + "; float angle ; angle = ( float ) - tilt ;", + tokenizeAndStringify("; float angle = (float) -tilt;", true)); + TODO_ASSERT_EQUALS("; float angle ; angle = tilt ;", + "; float angle ; angle = ( float ) + tilt ;", + tokenizeAndStringify("; float angle = (float) +tilt;", true)); + } + void inlineasm() { ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("asm { mov ax,bx };")); ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("_asm { mov ax,bx };"));