From bf8471e10904cb895bca3cf036db39794f6f6b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 11 May 2016 15:33:59 +0200 Subject: [PATCH] Fixed #7491 (Questionable enumMismatch) --- lib/checktype.cpp | 6 +++--- test/testtype.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 0b7eb84b9..7bb832622 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -338,16 +338,16 @@ void CheckType::checkEnumMismatch() } // Comparing enum variable against mismatching value - else if (tok->isComparisonOp()) { + else if (Token::Match(tok, "==|!=")) { if (!tok->astOperand1() || !tok->astOperand2()) continue; const ValueFlow::Value * const v1 = mismatchingValue(tok->astOperand1()->valueType(), tok->astOperand2()->values); - if (v1) + if (v1 && v1->isKnown()) enumMismatchCompareError(tok, *v1); const ValueFlow::Value * const v2 = mismatchingValue(tok->astOperand2()->valueType(), tok->astOperand1()->values); - if (v2) + if (v2 && v2->isKnown()) enumMismatchCompareError(tok, *v2); } } diff --git a/test/testtype.cpp b/test/testtype.cpp index 767e8460d..86ac3adb7 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -228,6 +228,13 @@ private: " if (5==abc) {}\n" "}", &settings); ASSERT_EQUALS("[test.cpp:3]: (style) Comparing mismatching value 5 with enum variable.\n", errout.str()); + + check("enum ABC {NEG1=-2,NEG2=-1,POS1=1,POS2=2};\n" // #7491 + "void f(enum ABC abc) {\n" + " if (abc>0) {}\n" + "}", &settings); + ASSERT_EQUALS("", errout.str()); + } };