diff --git a/lib/checktype.cpp b/lib/checktype.cpp index e3edbfbd4..e34e3e0fc 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -210,32 +210,27 @@ 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()) { - if (!tok->isArithmeticalOp() || Token::Match(tok,"+|-")) - continue; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { + if (!tok->isArithmeticalOp() || Token::Match(tok,"+|-")) + continue; - // Is result unsigned? - if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED)) - continue; + // Is result unsigned? + if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED)) + continue; - // Check if an operand can be negative.. - std::stack tokens; - tokens.push(tok->astOperand1()); - tokens.push(tok->astOperand2()); - while (!tokens.empty()) { - const Token *tok1 = tokens.top(); - tokens.pop(); - if (!tok1) - continue; - if (!tok1->getValueLE(-1,_settings)) - continue; - if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED) - signConversionError(tok1, tok1->isNumber()); - } + // Check if an operand can be negative.. + std::stack tokens; + tokens.push(tok->astOperand1()); + tokens.push(tok->astOperand2()); + while (!tokens.empty()) { + const Token *tok1 = tokens.top(); + tokens.pop(); + if (!tok1) + continue; + if (!tok1->getValueLE(-1,_settings)) + continue; + if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED) + signConversionError(tok1, tok1->isNumber()); } } } diff --git a/test/testtype.cpp b/test/testtype.cpp index 1d6a383c9..ca300a60e 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -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"