diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 53257591273..1247d97365e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2924,6 +2924,9 @@ void CheckOther::checkDuplicateExpression() if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=") && !isInsideLambdaCaptureList(tok)) { if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true)) continue; + if (tok->isArithmeticalOp() && + (tok->astOperand1()->isEnumerator() || (tok->astOperand2() && tok->astOperand2()->isEnumerator()))) + continue; const bool pointerDereference = (tok->astOperand1() && tok->astOperand1()->isUnaryOp("*")) || (tok->astOperand2() && tok->astOperand2()->isUnaryOp("*")); const bool followVar = (!isConstVarExpression(tok) || Token::Match(tok, "%comp%|%oror%|&&")) && !pointerDereference; diff --git a/test/testother.cpp b/test/testother.cpp index 50581a332c5..7a3f0a176cf 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -196,6 +196,7 @@ class TestOther : public TestFixture { TEST_CASE(duplicateExpression17); // #12036 TEST_CASE(duplicateExpression18); TEST_CASE(duplicateExpression19); + TEST_CASE(duplicateExpression20); TEST_CASE(duplicateExpressionLoop); TEST_CASE(duplicateValueTernary); TEST_CASE(duplicateValueTernarySizeof); // #13773 @@ -8010,6 +8011,12 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void duplicateExpression20() { + check("enum { N = 1 };\n" // #14202 + "int f() { return N - 1; }"); + ASSERT_EQUALS("", errout_str()); + } + void duplicateExpressionLoop() { check("void f() {\n" " int a = 1;\n"