diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 3a0453b85..9d6603b4f 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -268,6 +268,12 @@ void CheckType::checkLongCast() if (tok->str() != "=" || !Token::Match(tok->astOperand2(), "*|<<")) continue; + if (tok->astOperand2()->hasKnownIntValue()) { + const ValueFlow::Value &v = tok->astOperand2()->values().front(); + if (_settings->isIntValue(v.intvalue)) + continue; + } + const ValueType *lhstype = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr; const ValueType *rhstype = tok->astOperand2()->valueType(); diff --git a/test/testtype.cpp b/test/testtype.cpp index 9b2410186..3b329092f 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -232,6 +232,12 @@ private: "}\n", &settings); ASSERT_EQUALS("[test.cpp:2]: (style) int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information.\n", errout.str()); + check("long f() {\n" + " const long long ret = 256 * (1 << 10);\n" + " return ret;\n" + "}\n", &settings); + ASSERT_EQUALS("", errout.str()); + // typedef check("long f(int x, int y) {\n" " const size_t ret = x * y;\n"