@@ -1643,10 +1643,30 @@ namespace {
16431643 continue ;
16441644 }
16451645
1646+ const auto getExprIdForOperand = [](const Token* tok) -> int {
1647+ if (!tok)
1648+ return 0 ;
1649+
1650+ int otherExprId = 0 ;
1651+
1652+ // Look through all referenced tokens.
1653+ // If two exprIds are found and one matches tok->exprId(), return the other.
1654+ // Otherwise, default to returning tok->exprId().
1655+ for (const auto & ref: followAllReferences (tok)) {
1656+ const int refExprId = ref.token ->exprId ();
1657+ if (refExprId != 0 && refExprId != tok->exprId ()) {
1658+ if (otherExprId != 0 && otherExprId != refExprId)
1659+ return tok->exprId ();
1660+ otherExprId = refExprId;
1661+ }
1662+ }
1663+ return otherExprId != 0 ? otherExprId : tok->exprId ();
1664+ };
1665+
16461666 ExprIdKey key;
16471667 key.parentOp = tok->astParent ()->str ();
1648- key.operand1 = op1 ? op1-> exprId () : 0 ;
1649- key.operand2 = op2 ? op2-> exprId () : 0 ;
1668+ key.operand1 = getExprIdForOperand (op1) ;
1669+ key.operand2 = getExprIdForOperand (op2) ;
16501670
16511671 if (tok->astParent ()->isCast () && tok->astParent ()->str () == " (" ) {
16521672 const Token* typeStartToken;
@@ -1665,19 +1685,6 @@ namespace {
16651685 key.parentOp += type;
16661686 }
16671687
1668- for (const auto & ref: followAllReferences (op1)) {
1669- if (ref.token ->exprId () != 0 ) { // cppcheck-suppress useStlAlgorithm
1670- key.operand1 = ref.token ->exprId ();
1671- break ;
1672- }
1673- }
1674- for (const auto & ref: followAllReferences (op2)) {
1675- if (ref.token ->exprId () != 0 ) { // cppcheck-suppress useStlAlgorithm
1676- key.operand2 = ref.token ->exprId ();
1677- break ;
1678- }
1679- }
1680-
16811688 if (key.operand1 > key.operand2 && key.operand2 &&
16821689 Token::Match (tok->astParent (), " %or%|%oror%|+|*|&|&&|^|==|!=" )) {
16831690 // In C++ the order of operands of + might matter
0 commit comments