diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 256adf93d..d7f890ef9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6586,6 +6586,18 @@ bool Tokenizer::simplifyRedundantParenthesis() continue; } + if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) { + const Token *tok2 = tok->next(); + while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->isBoolean() || tok2->isArithmeticalOp())) + tok2 = tok2->next(); + if (tok2 && tok2->str() == ")") { + tok->link()->deleteThis(); + tok->deleteThis(); + ret = true; + continue; + } + } + while (Token::Match(tok->previous(), "[{([,:] ( !!{") && Token::Match(tok->link(), ") [;,])]")) { // We have "( ... )", remove the parenthesis tok->link()->deleteThis(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 234749350..24f75888d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4574,6 +4574,7 @@ private: void removeParentheses15() { ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);", false)); ASSERT_EQUALS("a = b ? c : 579 ;", tokenizeAndStringify("a = b ? c : ((123)+(456));", false)); + ASSERT_EQUALS("a = b ? 123 : c ;", tokenizeAndStringify("a = b ? (123) : c;", false)); } void tokenize_double() {