Refactor: Simplify checkSignConversion

The loop only checks astoperand1 and astoperand2. Simplify the condition
to loop over these instead of using a stack. Also, add a testcase for
when astoperand2 is negative.
This commit is contained in:
Rikard Falkeborn 2020-07-20 11:15:18 +02:00
parent b812d48397
commit 7973fd843c
2 changed files with 5 additions and 6 deletions

View File

@ -239,12 +239,8 @@ void CheckType::checkSignConversion()
continue; continue;
// Check if an operand can be negative.. // Check if an operand can be negative..
std::stack<const Token *> tokens; const Token * astOperands[] = { tok->astOperand1(), tok->astOperand2() };
tokens.push(tok->astOperand1()); for (const Token * tok1 : astOperands) {
tokens.push(tok->astOperand2());
while (!tokens.empty()) {
const Token *tok1 = tokens.top();
tokens.pop();
if (!tok1) if (!tok1)
continue; continue;
const ValueFlow::Value *negativeValue = tok1->getValueLE(-1,mSettings); const ValueFlow::Value *negativeValue = tok1->getValueLE(-1,mSettings);

View File

@ -252,6 +252,9 @@ private:
check("x = -4 * (unsigned)y;"); check("x = -4 * (unsigned)y;");
ASSERT_EQUALS("[test.cpp:1]: (warning) Expression '-4' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (warning) Expression '-4' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
check("x = (unsigned)y * -4;");
ASSERT_EQUALS("[test.cpp:1]: (warning) Expression '-4' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
check("unsigned int dostuff(int x) {\n" // x is signed check("unsigned int dostuff(int x) {\n" // x is signed
" if (x==0) {}\n" " if (x==0) {}\n"
" return (x-1)*sizeof(int);\n" " return (x-1)*sizeof(int);\n"