diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 1fde04836..e3e0db947 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -315,7 +315,9 @@ void CheckType::checkLongCast() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (!Token::Match(tok, "%var% =")) continue; - if (!tok->variable() || !tok->variable()->isConst() || tok->variable()->typeStartToken()->originalName() != "long") + if (!tok->variable() || !tok->variable()->isConst() || tok->variable()->typeStartToken()->str() != "long") + continue; + if (!tok->variable()->typeStartToken()->originalName().empty()) continue; if (Token::Match(tok->next()->astOperand2(), "*|<<") && astIsIntResult(tok->next()->astOperand2())) longCastAssignError(tok); @@ -331,7 +333,7 @@ void CheckType::checkLongCast() const Token * def = scope->classDef; bool islong = false; while (Token::Match(def, "%type%|::")) { - if (def->originalName() == "long") { + if (def->str() == "long" && def->originalName().empty()) { islong = true; break; } diff --git a/test/testtype.cpp b/test/testtype.cpp index c4f1b2878..5acc7795d 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -140,6 +140,7 @@ private: void longCastAssign() { Settings settings; settings.addEnabled("style"); + settings.platform(Settings::PlatformType::Unix64); check("long f(int x, int y) {\n" " const long ret = x * y;\n" @@ -147,6 +148,13 @@ private: "}\n", &settings); ASSERT_EQUALS("[test.cpp:2]: (style) possible loss of information, int result is assigned to long variable\n", errout.str()); + // typedef + check("long f(int x, int y) {\n" + " const size_t ret = x * y;\n" + " return ret;\n" + "}\n", &settings); + ASSERT_EQUALS("", errout.str()); + // astIsIntResult check("long f(int x, int y) {\n" " const long ret = (long)x * y;\n" @@ -163,6 +171,12 @@ private: " return x * y;\n" "}\n", &settings); ASSERT_EQUALS("[test.cpp:2]: (style) possible loss of information, int result is returned as long value\n", errout.str()); + + // typedef + check("size_t f(int x, int y) {\n" + " return x * y;\n" + "}\n", &settings); + ASSERT_EQUALS("", errout.str()); } };