diff --git a/lib/checktype.cpp b/lib/checktype.cpp index c9c9179f0..2faa922e5 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -238,7 +238,8 @@ void CheckType::checkSignConversion() // Check if there are signed operands that can be negative.. std::stack tokens; - tokens.push(tok); + tokens.push(tok->astOperand1()); + tokens.push(tok->astOperand2()); while (!tokens.empty()) { const Token *tok1 = tokens.top(); tokens.pop(); @@ -268,8 +269,6 @@ void CheckType::checkSignConversion() break; } } - tokens.push(tok1->astOperand1()); - tokens.push(tok1->astOperand2()); } } } diff --git a/test/testtype.cpp b/test/testtype.cpp index 31d27017c..b36b780b3 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -117,6 +117,12 @@ private: "void f2() { f1(-4); }"); ASSERT_EQUALS("[test.cpp:1]: (warning) Suspicious code: sign conversion of x in calculation, even though x can have a negative value\n", errout.str()); + check("unsigned int f1(int x) {" // #6168: FP for inner calculation + " return 5U * (1234 - x);\n" // <- signed subtraction, x is not sign converted + "}\n" + "void f2() { f1(-4); }"); + ASSERT_EQUALS("", errout.str()); + // Dont warn for + and - check("void f1(int x) {" " a = x + 5U;\n"