diff --git a/src/checkother.cpp b/src/checkother.cpp index d582f12ff..491439cea 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -308,27 +308,25 @@ void CheckOther::checkUnsignedDivision() return; // Check for "ivar / uvar" and "uvar / ivar" - std::map varsign; + std::map varsign; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]")) { const char *type = tok->strAt(1); if (strcmp(type, "char") == 0 || strcmp(type, "short") == 0 || strcmp(type, "int") == 0) - varsign[tok->strAt(2)] = 's'; + varsign[tok->tokAt(2)->varId()] = 's'; } else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]")) - varsign[tok->strAt(3)] = 'u'; + varsign[tok->tokAt(3)->varId()] = 'u'; else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %var%")) { if (ErrorLogger::udivWarning(*_settings)) { - const char *varname1 = tok->strAt(1); - const char *varname2 = tok->strAt(3); - char sign1 = varsign[varname1]; - char sign2 = varsign[varname2]; + char sign1 = varsign[tok->tokAt(1)->varId()]; + char sign2 = varsign[tok->tokAt(3)->varId()]; if (sign1 && sign2 && sign1 != sign2) { @@ -342,8 +340,7 @@ void CheckOther::checkUnsignedDivision() { if (tok->strAt(3)[0] == '-' && ErrorLogger::udivError()) { - const char *varname1 = tok->strAt(1); - char sign1 = varsign[varname1]; + char sign1 = varsign[tok->tokAt(1)->varId()]; if (sign1 == 'u') { udivError(tok->next()); @@ -355,8 +352,7 @@ void CheckOther::checkUnsignedDivision() { if (tok->strAt(1)[0] == '-' && ErrorLogger::udivError()) { - const char *varname2 = tok->strAt(3); - char sign2 = varsign[varname2]; + char sign2 = varsign[tok->tokAt(3)->varId()]; if (sign2 == 'u') { udivError(tok->next()); diff --git a/test/testdivision.cpp b/test/testdivision.cpp index 8c6bcc39e..5bc17b041 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -179,6 +179,14 @@ private: " }\n" "}\n", true, true); ASSERT_EQUALS("[test.cpp:6]: (possible style) Division with signed and unsigned operators\n", errout.str()); + + check("void a(int i) { }\n" + "int foo( unsigned int sz )\n" + "{\n" + " register unsigned int i=1;\n" + " return i/sz;\n" + "}\n", true, true); + ASSERT_EQUALS("", errout.str()); } };