Skip to content

Commit

Permalink
Improve fix for #9570: check if ternary operator is used in assignmen…
Browse files Browse the repository at this point in the history
…t to reference (#3614)
  • Loading branch information
chrchr-github authored Dec 16, 2021
1 parent c02dd5b commit e8260f2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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);
if (v1 == tok1->values().end()) {
return false;
Expand Down Expand Up @@ -2114,6 +2111,10 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
{
if (!tok)
return false;

if (indirect == 0 && isConstVarExpression(tok))
return false;

const Token *tok2 = tok;
int derefs = 0;
while (Token::simpleMatch(tok2->astParent(), "*") ||
Expand Down Expand Up @@ -2582,6 +2583,8 @@ bool isConstVarExpression(const Token *tok, const char* skipMatch)
{
if (!tok)
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))
return false;
if (Token::simpleMatch(tok->previous(), "sizeof ("))
Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,7 @@ void CheckOther::checkDuplicateExpression()
}
}
} 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);
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
duplicateExpressionTernaryError(tok, errorPath);
Expand Down

0 comments on commit e8260f2

Please sign in to comment.