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:
parent
b812d48397
commit
7973fd843c
|
@ -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);
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Reference in New Issue