Skip to content

Commit e8260f2

Browse files
Improve fix for #9570: check if ternary operator is used in assignment to reference (#3614)
1 parent c02dd5b commit e8260f2

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/astutils.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,6 @@ static bool compareKnownValue(const Token * const tok1, const Token * const tok2
10361036
{
10371037
static const auto isKnownFn = std::mem_fn(&ValueFlow::Value::isKnown);
10381038

1039-
if ((tok1->variable() && tok1->variable()->isStatic()) || (tok2->variable() && tok2->variable()->isStatic()))
1040-
return false;
1041-
10421039
const auto v1 = std::find_if(tok1->values().begin(), tok1->values().end(), isKnownFn);
10431040
if (v1 == tok1->values().end()) {
10441041
return false;
@@ -2114,6 +2111,10 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
21142111
{
21152112
if (!tok)
21162113
return false;
2114+
2115+
if (indirect == 0 && isConstVarExpression(tok))
2116+
return false;
2117+
21172118
const Token *tok2 = tok;
21182119
int derefs = 0;
21192120
while (Token::simpleMatch(tok2->astParent(), "*") ||
@@ -2582,6 +2583,8 @@ bool isConstVarExpression(const Token *tok, const char* skipMatch)
25822583
{
25832584
if (!tok)
25842585
return false;
2586+
if (tok->str() == "?" && tok->astOperand2() && tok->astOperand2()->str() == ":") // ternary operator
2587+
return isConstVarExpression(tok->astOperand2()->astOperand1()) && isConstVarExpression(tok->astOperand2()->astOperand2()); // left and right of ":"
25852588
if (skipMatch && Token::Match(tok, skipMatch))
25862589
return false;
25872590
if (Token::simpleMatch(tok->previous(), "sizeof ("))

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ void CheckOther::checkDuplicateExpression()
22932293
}
22942294
}
22952295
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
2296-
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
2296+
if (!isVariableChanged(tok->astParent(), /*indirect*/ 0, mSettings, mTokenizer->isCPP()) && !tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
22972297
duplicateValueTernaryError(tok);
22982298
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
22992299
duplicateExpressionTernaryError(tok, errorPath);

0 commit comments

Comments
 (0)