Fixed #6168 (False positive: sign conversion for inner calculation)
This commit is contained in:
parent
865a252c89
commit
d354cdc02c
|
@ -238,7 +238,8 @@ void CheckType::checkSignConversion()
|
||||||
|
|
||||||
// Check if there are signed operands that can be negative..
|
// Check if there are signed operands that can be negative..
|
||||||
std::stack<const Token *> tokens;
|
std::stack<const Token *> tokens;
|
||||||
tokens.push(tok);
|
tokens.push(tok->astOperand1());
|
||||||
|
tokens.push(tok->astOperand2());
|
||||||
while (!tokens.empty()) {
|
while (!tokens.empty()) {
|
||||||
const Token *tok1 = tokens.top();
|
const Token *tok1 = tokens.top();
|
||||||
tokens.pop();
|
tokens.pop();
|
||||||
|
@ -268,8 +269,6 @@ void CheckType::checkSignConversion()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tokens.push(tok1->astOperand1());
|
|
||||||
tokens.push(tok1->astOperand2());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,6 +117,12 @@ private:
|
||||||
"void f2() { f1(-4); }");
|
"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());
|
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 -
|
// Dont warn for + and -
|
||||||
check("void f1(int x) {"
|
check("void f1(int x) {"
|
||||||
" a = x + 5U;\n"
|
" a = x + 5U;\n"
|
||||||
|
|
Loading…
Reference in New Issue