From e2069dd1b9224d433d76195bbe277470f48f25ed Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 4 May 2022 14:25:00 +0200 Subject: [PATCH] Fix #10650 FN knownConditionTrueFalse with const int value (#4078) --- lib/astutils.cpp | 10 +++++++++- test/testother.cpp | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e06f9f495..0cf7e41ff 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1148,11 +1148,19 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token }); } -static inline bool isSameConstantValue(bool macro, const Token * const tok1, const Token * const tok2) +static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2) { if (tok1 == nullptr || tok2 == nullptr) return false; + auto adjustForCast = [](const Token* tok) { + if (Token::Match(tok->previous(), "%type% (|{") && tok->previous()->isStandardType() && tok->astOperand2()) + return tok->astOperand2(); + return tok; + }; + tok1 = adjustForCast(tok1); + tok2 = adjustForCast(tok2); + if (!tok1->isNumber() || !tok2->isNumber()) return false; diff --git a/test/testother.cpp b/test/testother.cpp index c946fa7c8..21da5c39d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -159,6 +159,7 @@ private: TEST_CASE(duplicateExpression12); // #10026 TEST_CASE(duplicateExpression13); // #7899 TEST_CASE(duplicateExpression14); // #9871 + TEST_CASE(duplicateExpression15); // #10650 TEST_CASE(duplicateExpressionLoop); TEST_CASE(duplicateValueTernary); TEST_CASE(duplicateExpressionTernary); // #6391 @@ -5757,6 +5758,20 @@ private: ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4] -> [test.cpp:5]: (style) The comparison 'f+4 != g+4' is always false because 'f+4' and 'g+4' represent the same value.\n", errout.str()); } + void duplicateExpression15() { //#10650 + check("bool f() {\n" + " const int i = int(0);\n" + " return i == 0;\n" + "}\n" + "bool g() {\n" + " const int i = int{ 0 };\n" + " return i == 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i == 0' is always true.\n" + "[test.cpp:6] -> [test.cpp:7]: (style) The comparison 'i == 0' is always true.\n", + errout.str()); + } + void duplicateExpressionLoop() { check("void f() {\n" " int a = 1;\n"