From 47062e8a5c1ad219ba76b7e7d3e8e207a7a808b4 Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Mon, 9 Feb 2026 16:54:18 +0100 Subject: [PATCH 1/4] 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 3b9c88739e9b76ebe15a913b634ef59a3a507caa Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Mon, 16 Feb 2026 15:28:41 +0100 Subject: [PATCH 2/4] 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" From cd494b2d4ec7edf876af3085e163c8925fa7db99 Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Fri, 20 Feb 2026 09:57:14 +0100 Subject: [PATCH 3/4] Added - Check known value also in integer range --- lib/checktype.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 025c14b4620..509d13b54e0 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -383,9 +383,12 @@ void CheckType::checkLongCast() const ValueType *type = tok->astOperand1()->valueType(); if (type && checkTypeCombination(*type, *retVt, *mSettings) && type->pointer == 0U && - type->originalTypeName.empty() && - !tok->astOperand1()->hasKnownIntValue()) - ret = tok; + type->originalTypeName.empty()) { + if (!tok->astOperand1()->hasKnownIntValue()) { + ret = tok; + } else if (!mSettings->platform.isIntValue(tok->astOperand1()->getKnownIntValue())) + ret = tok; + } } // All return statements must have problem otherwise no warning if (ret != tok) { From 976db098dc2de0c87f922b9366a86611666f92f3 Mon Sep 17 00:00:00 2001 From: Johan Crone Date: Fri, 20 Feb 2026 09:57:50 +0100 Subject: [PATCH 4/4] Added new testcase for known value outside integer range --- test/testtype.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testtype.cpp b/test/testtype.cpp index 8873956217b..ea31fbdc313 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -478,6 +478,13 @@ class TestType : public TestFixture { 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()); + const char code5[] = "long f() {\n" + " unsigned int n = 1U << 20;\n" + " return n << 20;\n" + "}\n"; + check(code5, dinit(CheckOptions, $.settings = &settings)); + ASSERT_EQUALS("[test.cpp:3: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"