Fix #12164 (False positive: operator precedence warning even though there are parentheses) (#5640)

This commit is contained in:
Daniel Marjamäki 2023-11-08 18:54:40 +01:00 committed by GitHub
parent 617b7a39cd
commit 8f432880fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 13 deletions

View File

@ -7835,18 +7835,6 @@ bool Tokenizer::simplifyRedundantParentheses()
ret = true;
}
if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) {
const Token *tok2 = tok->next();
while (tok2 && (Token::Match(tok2,"%bool%|%num%|%name%") || 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(), ") [;,])]") &&
!Token::simpleMatch(tok->tokAt(-2), "operator ,") && // Ticket #5709

View File

@ -171,6 +171,7 @@ private:
TEST_CASE(removeParentheses25); // daca@home - a=(b,c)
TEST_CASE(removeParentheses26); // Ticket #8875 a[0](0)
TEST_CASE(removeParentheses27);
TEST_CASE(removeParentheses28); // #12164 - don't remove parentheses in '(expr1) ? (expr2) : (expr3);'
TEST_CASE(tokenize_double);
TEST_CASE(tokenize_strings);
@ -1809,7 +1810,7 @@ private:
void removeParentheses15() {
ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);"));
ASSERT_EQUALS("a = b ? c : ( 123 + 456 ) ;", tokenizeAndStringify("a = b ? c : ((123)+(456));"));
ASSERT_EQUALS("a = b ? 123 : c ;", tokenizeAndStringify("a = b ? (123) : c;"));
ASSERT_EQUALS("a = b ? ( 123 ) : c ;", tokenizeAndStringify("a = b ? (123) : c;"));
// #4316
ASSERT_EQUALS("a = b ? c : ( d = 1 , 0 ) ;", tokenizeAndStringify("a = b ? c : (d=1,0);"));
@ -1918,6 +1919,12 @@ private:
ASSERT_EQUALS(exp, tokenizeAndStringify(code));
}
void removeParentheses28() { // Ticket #12164
static char code[] = "temp1 = (value > 100U) ? (value+100U) : (value-50U);";
static const char exp[] = "temp1 = ( value > 100U ) ? ( value + 100U ) : ( value - 50U ) ;";
ASSERT_EQUALS(exp, tokenizeAndStringify(code));
}
void tokenize_double() {
const char code[] = "void f() {\n"
" double a = 4.2;\n"