Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,9 +1037,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