Skip to content

Commit e2069dd

Browse files
Fix #10650 FN knownConditionTrueFalse with const int value (#4078)
1 parent 767b12b commit e2069dd

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,11 +1148,19 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token
11481148
});
11491149
}
11501150

1151-
static inline bool isSameConstantValue(bool macro, const Token * const tok1, const Token * const tok2)
1151+
static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2)
11521152
{
11531153
if (tok1 == nullptr || tok2 == nullptr)
11541154
return false;
11551155

1156+
auto adjustForCast = [](const Token* tok) {
1157+
if (Token::Match(tok->previous(), "%type% (|{") && tok->previous()->isStandardType() && tok->astOperand2())
1158+
return tok->astOperand2();
1159+
return tok;
1160+
};
1161+
tok1 = adjustForCast(tok1);
1162+
tok2 = adjustForCast(tok2);
1163+
11561164
if (!tok1->isNumber() || !tok2->isNumber())
11571165
return false;
11581166

test/testother.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class TestOther : public TestFixture {
159159
TEST_CASE(duplicateExpression12); // #10026
160160
TEST_CASE(duplicateExpression13); // #7899
161161
TEST_CASE(duplicateExpression14); // #9871
162+
TEST_CASE(duplicateExpression15); // #10650
162163
TEST_CASE(duplicateExpressionLoop);
163164
TEST_CASE(duplicateValueTernary);
164165
TEST_CASE(duplicateExpressionTernary); // #6391
@@ -5757,6 +5758,20 @@ class TestOther : public TestFixture {
57575758
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());
57585759
}
57595760

5761+
void duplicateExpression15() { //#10650
5762+
check("bool f() {\n"
5763+
" const int i = int(0);\n"
5764+
" return i == 0;\n"
5765+
"}\n"
5766+
"bool g() {\n"
5767+
" const int i = int{ 0 };\n"
5768+
" return i == 0;\n"
5769+
"}\n");
5770+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i == 0' is always true.\n"
5771+
"[test.cpp:6] -> [test.cpp:7]: (style) The comparison 'i == 0' is always true.\n",
5772+
errout.str());
5773+
}
5774+
57605775
void duplicateExpressionLoop() {
57615776
check("void f() {\n"
57625777
" int a = 1;\n"

0 commit comments

Comments
 (0)