checkSignConversion: check all calculations, not only in function bodies

This commit is contained in:
Daniel Marjamäki 2017-09-19 19:25:33 +02:00
parent f6e30eee19
commit 9268c2034a
2 changed files with 22 additions and 24 deletions

View File

@ -210,11 +210,7 @@ void CheckType::checkSignConversion()
if (!_settings->isEnabled(Settings::WARNING))
return;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (!tok->isArithmeticalOp() || Token::Match(tok,"+|-"))
continue;
@ -238,7 +234,6 @@ void CheckType::checkSignConversion()
}
}
}
}
void CheckType::signConversionError(const Token *tok, const bool constvalue)
{

View File

@ -148,6 +148,9 @@ private:
}
void signConversion() {
check("x = -4 * (unsigned)y;");
ASSERT_EQUALS("[test.cpp:1]: (warning) Suspicious code: sign conversion of -4 in calculation because '-4' has a negative value\n", errout.str());
check("unsigned int f1(signed int x, unsigned int y) {" // x is signed
" return x * y;\n"
"}\n"