diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 36faf837f..2185d7ba0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -294,7 +294,15 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog val2.varId = varid; } } - + if (Token::Match(tok,"<|>")) { + if (num!=0) + continue; + bool isunsigned = false; + for (const Token* type = var->typeStartToken(); type && type->varId() == 0U; type = type->next()) + isunsigned |= type->isUnsigned(); + if (!isunsigned) + continue; + } for (Token *tok2 = tok->previous(); ; tok2 = tok2->previous()) { if (!tok2) { if (settings->debugwarnings) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index bbc1129d0..2ab8db046 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -157,6 +157,24 @@ private: ASSERT_EQUALS(true, testValueOfX(code, 2U, 1)); ASSERT_EQUALS(true, testValueOfX(code, 2U, 0)); + code = "void f(unsigned int x) {\n" + " int a = x;\n" + " if (x > 0) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 2U, 0)); + + code = "void f(unsigned int x) {\n" + " int a = x;\n" + " if (x > 1) {}\n" // not zero => don't consider > condition + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 2U, 1)); + + code = "void f(int x) {\n" // not unsigned => don't consider > condition + " int a = x;\n" + " if (x > 0) {}\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 2U, 0)); + code = "void f(int *x) {\n" " *x = 100;\n" " if (x) {}\n"