diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4122bf640..d1258a6f1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2030,8 +2030,8 @@ 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())) duplicateValueTernaryError(tok); - else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, false)) - duplicateExpressionTernaryError(tok); + else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath)) + duplicateExpressionTernaryError(tok, errorPath); } } } @@ -2086,9 +2086,10 @@ void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token * "determine if it is correct.", CWE398, false); } -void CheckOther::duplicateExpressionTernaryError(const Token *tok) +void CheckOther::duplicateExpressionTernaryError(const Token *tok, ErrorPath errors) { - reportError(tok, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n" + errors.emplace_back(tok, ""); + reportError(errors, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n" "Finding the same expression in both branches of ternary operator is suspicious as " "the same code is executed regardless of the condition.", CWE398, false); } diff --git a/lib/checkother.h b/lib/checkother.h index 9d275c52a..ddae3aee7 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -242,7 +242,7 @@ private: void oppositeExpressionError(const Token *tok1, const Token *tok2, const std::string &op, ErrorPath errors); void duplicateExpressionError(const Token *tok1, const Token *tok2, const Token *opTok, ErrorPath errors); void duplicateValueTernaryError(const Token *tok); - void duplicateExpressionTernaryError(const Token *tok); + void duplicateExpressionTernaryError(const Token *tok, ErrorPath errors); void duplicateBreakError(const Token *tok, bool inconclusive); void unreachableCodeError(const Token* tok, bool inconclusive); void unsignedLessThanZeroError(const Token *tok, const std::string &varname, bool inconclusive); @@ -305,7 +305,7 @@ private: c.oppositeExpressionError(nullptr, nullptr, "&&", errorPath); c.duplicateExpressionError(nullptr, nullptr, nullptr, errorPath); c.duplicateValueTernaryError(nullptr); - c.duplicateExpressionTernaryError(nullptr); + c.duplicateExpressionTernaryError(nullptr, errorPath); c.duplicateBreakError(nullptr, false); c.unreachableCodeError(nullptr, false); c.unsignedLessThanZeroError(nullptr, "varname", false); diff --git a/test/testother.cpp b/test/testother.cpp index 84b7ec917..78fd3030c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4162,6 +4162,12 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Same expression in both branches of ternary operator.\n", errout.str()); + check("int f(bool b, int a) {\n" + " const int c = a;\n" + " return b ? a : c;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Same expression in both branches of ternary operator.\n", errout.str()); + check("void f() {\n" " return A ? x : z;\n" "}");