From 8c924bf33ce3db323f794207cb81924ed94bc848 Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Mon, 9 Feb 2026 16:54:18 +0100 Subject: [PATCH 1/2] Do not warn for truncLongCastReturn if operands have known valid int value --- lib/checktype.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 68b6580d270..025c14b4620 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -383,7 +383,8 @@ void CheckType::checkLongCast() const ValueType *type = tok->astOperand1()->valueType(); if (type && checkTypeCombination(*type, *retVt, *mSettings) && type->pointer == 0U && - type->originalTypeName.empty()) + type->originalTypeName.empty() && + !tok->astOperand1()->hasKnownIntValue()) ret = tok; } // All return statements must have problem otherwise no warning From 88ba04e954adaa4e43c0a26f3b4a9b3042f42a7e Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Mon, 16 Feb 2026 15:28:41 +0100 Subject: [PATCH 2/2] Added test case with and without long cast warning --- test/testtype.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testtype.cpp b/test/testtype.cpp index b8cfae820d5..8873956217b 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -465,6 +465,19 @@ class TestType : public TestFixture { check(code2, dinit(CheckOptions, $.settings = &settingsWin)); ASSERT_EQUALS("[test.cpp:2:3]: (style) int result is returned as long long value. If the return value is long long to avoid loss of information, then you have loss of information. [truncLongCastReturn]\n", errout_str()); + const char code3[] = "long f() {\n" + " int n = 1;\n" + " return n << 12;\n" + "}\n"; + check(code3, dinit(CheckOptions, $.settings = &settings)); + ASSERT_EQUALS("", errout_str()); + + const char code4[] = "long f(int n) {\n" + " return n << 12;\n" + "}\n"; + check(code4, dinit(CheckOptions, $.settings = &settings)); + ASSERT_EQUALS("[test.cpp:2:5]: (style) int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]\n", errout_str()); + // typedef check("size_t f(int x, int y) {\n" " return x * y;\n"