Improve fix for #9570: check if ternary operator is used in assignment to reference (#3614)

This commit is contained in:
chrchr-github 2021-12-16 22:32:15 +01:00 committed by GitHub
parent c02dd5bf23
commit e8260f2dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -1036,9 +1036,6 @@ static bool compareKnownValue(const Token * const tok1, const Token * const tok2
{ {
static const auto isKnownFn = std::mem_fn(&ValueFlow::Value::isKnown); static const auto isKnownFn = std::mem_fn(&ValueFlow::Value::isKnown);
if ((tok1->variable() && tok1->variable()->isStatic()) || (tok2->variable() && tok2->variable()->isStatic()))
return false;
const auto v1 = std::find_if(tok1->values().begin(), tok1->values().end(), isKnownFn); const auto v1 = std::find_if(tok1->values().begin(), tok1->values().end(), isKnownFn);
if (v1 == tok1->values().end()) { if (v1 == tok1->values().end()) {
return false; return false;
@ -2114,6 +2111,10 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
{ {
if (!tok) if (!tok)
return false; return false;
if (indirect == 0 && isConstVarExpression(tok))
return false;
const Token *tok2 = tok; const Token *tok2 = tok;
int derefs = 0; int derefs = 0;
while (Token::simpleMatch(tok2->astParent(), "*") || while (Token::simpleMatch(tok2->astParent(), "*") ||
@ -2582,6 +2583,8 @@ bool isConstVarExpression(const Token *tok, const char* skipMatch)
{ {
if (!tok) if (!tok)
return false; return false;
if (tok->str() == "?" && tok->astOperand2() && tok->astOperand2()->str() == ":") // ternary operator
return isConstVarExpression(tok->astOperand2()->astOperand1()) && isConstVarExpression(tok->astOperand2()->astOperand2()); // left and right of ":"
if (skipMatch && Token::Match(tok, skipMatch)) if (skipMatch && Token::Match(tok, skipMatch))
return false; return false;
if (Token::simpleMatch(tok->previous(), "sizeof (")) if (Token::simpleMatch(tok->previous(), "sizeof ("))

View File

@ -2293,7 +2293,7 @@ void CheckOther::checkDuplicateExpression()
} }
} }
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") { } else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2())) if (!isVariableChanged(tok->astParent(), /*indirect*/ 0, mSettings, mTokenizer->isCPP()) && !tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
duplicateValueTernaryError(tok); duplicateValueTernaryError(tok);
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath)) else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
duplicateExpressionTernaryError(tok, errorPath); duplicateExpressionTernaryError(tok, errorPath);