Merge pull request #2716 from rikardfalkeborn/refactor-use-simple-loop-instead-of-stack-in-checktype

Refactor: Simplify checkSignConversion
This commit is contained in:
Daniel Marjamäki 2020-07-21 15:26:03 +02:00 committed by GitHub
commit 26c849b732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"