Skip to content

Commit 787da43

Browse files
Fix #12036 FN knownConditionTrueFalse comparing enum with number (#5510)
1 parent a52d2a2 commit 787da43

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

lib/astutils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,10 +1403,10 @@ static inline bool isSameConstantValue(bool macro, const Token* tok1, const Toke
14031403
};
14041404

14051405
tok1 = adjustForCast(tok1);
1406-
if (!tok1->isNumber())
1406+
if (!tok1->isNumber() && !tok1->enumerator())
14071407
return false;
14081408
tok2 = adjustForCast(tok2);
1409-
if (!tok2->isNumber())
1409+
if (!tok2->isNumber() && !tok2->enumerator())
14101410
return false;
14111411

14121412
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
@@ -1523,7 +1523,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
15231523
return true;
15241524

15251525
// Follow variable
1526-
if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId())) {
1526+
if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId() || tok1->enumerator() || tok2->enumerator())) {
15271527
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
15281528
if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) {
15291529
followVariableExpressionError(tok1, varTok1, errors);

lib/checkother.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,10 +2595,11 @@ void CheckOther::checkDuplicateExpression()
25952595
if (isWithoutSideEffects(cpp, tok->astOperand1())) {
25962596
const Token* loopTok = isInLoopCondition(tok);
25972597
if (!loopTok || !isExpressionChanged(tok, tok, loopTok->link()->next()->link(), mSettings, cpp)) {
2598-
const bool assignment = tok->str() == "=";
2598+
const bool isEnum = tok->scope()->type == Scope::eEnum;
2599+
const bool assignment = !isEnum && tok->str() == "=";
25992600
if (assignment && warningEnabled)
26002601
selfAssignmentError(tok, tok->astOperand1()->expressionString());
2601-
else if (styleEnabled) {
2602+
else if (styleEnabled && !isEnum) {
26022603
if (cpp && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
26032604
const Token* parent = tok->astParent();
26042605
while (parent && parent->astParent()) {

test/testother.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class TestOther : public TestFixture {
175175
TEST_CASE(duplicateExpression14); // #9871
176176
TEST_CASE(duplicateExpression15); // #10650
177177
TEST_CASE(duplicateExpression16); // #10569
178+
TEST_CASE(duplicateExpression17); // #12036
178179
TEST_CASE(duplicateExpressionLoop);
179180
TEST_CASE(duplicateValueTernary);
180181
TEST_CASE(duplicateExpressionTernary); // #6391
@@ -6291,7 +6292,8 @@ class TestOther : public TestFixture {
62916292
" enum { Four = 4 };\n"
62926293
" if (Four == 4) {}"
62936294
"}", nullptr, true, false);
6294-
ASSERT_EQUALS("", errout.str());
6295+
ASSERT_EQUALS("[test.cpp:3]: (style) The comparison 'Four == 4' is always true.\n",
6296+
errout.str());
62956297

62966298
check("void f() {\n"
62976299
" enum { Four = 4 };\n"
@@ -6310,7 +6312,8 @@ class TestOther : public TestFixture {
63106312
" enum { FourInEnumTwo = 4 };\n"
63116313
" if (FourInEnumOne == FourInEnumTwo) {}\n"
63126314
"}", nullptr, true, false);
6313-
ASSERT_EQUALS("", errout.str());
6315+
ASSERT_EQUALS("[test.cpp:4]: (style) The comparison 'FourInEnumOne == FourInEnumTwo' is always true because 'FourInEnumOne' and 'FourInEnumTwo' represent the same value.\n",
6316+
errout.str());
63146317

63156318
check("void f() {\n"
63166319
" enum { FourInEnumOne = 4 };\n"
@@ -6853,6 +6856,19 @@ class TestOther : public TestFixture {
68536856
ASSERT_EQUALS("[test.cpp:3]: (style) Same expression '!s[1]' found multiple times in chain of '||' operators.\n", errout.str());
68546857
}
68556858

6859+
void duplicateExpression17() {
6860+
check("enum { E0 };\n" // #12036
6861+
"void f() {\n"
6862+
" if (0 > E0) {}\n"
6863+
" if (E0 > 0) {}\n"
6864+
" if (E0 == 0) {}\n"
6865+
"}\n");
6866+
ASSERT_EQUALS("[test.cpp:3]: (style) The comparison '0 > E0' is always false.\n"
6867+
"[test.cpp:4]: (style) The comparison 'E0 > 0' is always false.\n"
6868+
"[test.cpp:5]: (style) The comparison 'E0 == 0' is always true.\n",
6869+
errout.str());
6870+
}
6871+
68566872
void duplicateExpressionLoop() {
68576873
check("void f() {\n"
68586874
" int a = 1;\n"

0 commit comments

Comments
 (0)