From c5180d725dd8373761add7642b324d12e2d2fcd1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:12:40 +0200 Subject: [PATCH 1/5] Update checkother.cpp --- lib/checkother.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4476814fcbb..c4ac64ec86c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -4226,7 +4226,7 @@ static const Token *findShadowed(const Scope *scope, const Variable& var, int li if (!scope) return nullptr; for (const Variable &v : scope->varlist) { - if (scope->isExecutable() && v.nameToken()->linenr() > linenr) + if ((scope->isExecutable() || scope->type == ScopeType::eGlobal) && v.nameToken()->linenr() > linenr) continue; if (v.name() == var.name()) return v.nameToken(); @@ -4312,9 +4312,10 @@ void CheckOtherImpl::checkShadowVariables() void CheckOtherImpl::shadowError(const Token *shadows, const std::string &shadowsType, const Token *shadowed, const std::string &shadowedType) { - ErrorPath errorPath; - errorPath.emplace_back(shadowed, "Shadowed " + shadowedType); - errorPath.emplace_back(shadows, "Shadow " + shadowsType); + ErrorPath errorPath{ + {shadowed, "Shadowed " + shadowedType}, + {shadows, "Shadow " + shadowsType} + }; const std::string &varname = shadows ? shadows->str() : shadowsType; const std::string ShadowsType = char(std::toupper(shadowsType[0])) + shadowsType.substr(1); const std::string ShadowedType = char(std::toupper(shadowedType[0])) + shadowedType.substr(1); From 007ab4b10a5a943e81adac9792a1afb05536675b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:14:47 +0200 Subject: [PATCH 2/5] Update testother.cpp --- test/testother.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 60c2a3bc528..6b22ad6980e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13520,6 +13520,13 @@ class TestOther : public TestFixture { check("struct S { void g(float f) {} void f() {} };\n"); ASSERT_EQUALS("[test.cpp:1:36] -> [test.cpp:1:25]: (style) Argument 'f' shadows outer function [shadowFunction]\n", errout_str()); + + check("int f() {\n" // #15114 + " int i = 0;\n" + " return i;\n" + "}\n" + "int i = 1;\n"); + ASSERT_EQUALS("", errout_str()); } void knownArgument() { From 59569d5a9d43a21e6b4b95fa758315ae048b624b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 19 Oct 2026 16:24:17 +0200 Subject: [PATCH 3/5] Update checkother.cpp --- lib/checkother.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c4ac64ec86c..9b6661057fd 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -4221,12 +4221,12 @@ void CheckOtherImpl::funcArgOrderDifferent(const std::string & functionName, reportError(tokens, Severity::warning, "funcArgOrderDifferent", msg, CWE683, Certainty::normal); } -static const Token *findShadowed(const Scope *scope, const Variable& var, int linenr) +static const Token *findShadowed(const Scope *scope, const Variable& var) { if (!scope) return nullptr; for (const Variable &v : scope->varlist) { - if ((scope->isExecutable() || scope->type == ScopeType::eGlobal) && v.nameToken()->linenr() > linenr) + if ((scope->isExecutable() || scope->type == ScopeType::eGlobal) && succeeds(v.nameToken(), var.nameToken())) continue; if (v.name() == var.name()) return v.nameToken(); @@ -4240,9 +4240,9 @@ static const Token *findShadowed(const Scope *scope, const Variable& var, int li if (scope->type == ScopeType::eLambda) return nullptr; - const Token* shadowed = findShadowed(scope->nestedIn, var, linenr); + const Token* shadowed = findShadowed(scope->nestedIn, var); if (!shadowed) - shadowed = findShadowed(scope->functionOf, var, linenr); + shadowed = findShadowed(scope->functionOf, var); return shadowed; } @@ -4276,9 +4276,9 @@ void CheckOtherImpl::checkShadowVariables() } } - const Token *shadowed = findShadowed(scope.nestedIn, var, var.nameToken()->linenr()); + const Token *shadowed = findShadowed(scope.nestedIn, var); if (!shadowed) - shadowed = findShadowed(scope.functionOf, var, var.nameToken()->linenr()); + shadowed = findShadowed(scope.functionOf, var); if (!shadowed) return; if (scope.type == ScopeType::eFunction && scope.className == var.name()) From 480e5d0eeb83255bf1bcd8016138da60c96ae7fd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 19 Oct 2026 16:25:11 +0200 Subject: [PATCH 4/5] Update testother.cpp --- test/testother.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 6b22ad6980e..5a51ccdff69 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13527,6 +13527,18 @@ class TestOther : public TestFixture { "}\n" "int i = 1;\n"); ASSERT_EQUALS("", errout_str()); + + { + ScopedFile inc("test.h", "int pad1;\nint pad2;\nint pad3;\nint pad4;\nfloat f;\n"); + checkP("void g() {\n" + " #include \"test.h\"\n" + " {\n" + " float f;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.h:5:7] -> [test.cpp:4:15]: (style) Local variable 'f' shadows outer variable [shadowVariable]\n", errout_str()); + } + } void knownArgument() { From 7b8c53c3b3e54fccc9835b8d915ed390a6c3d823 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 19 Oct 2026 16:45:22 +0200 Subject: [PATCH 5/5] Update testother.cpp --- test/testother.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 5a51ccdff69..6e525ef39bd 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13531,14 +13531,13 @@ class TestOther : public TestFixture { { ScopedFile inc("test.h", "int pad1;\nint pad2;\nint pad3;\nint pad4;\nfloat f;\n"); checkP("void g() {\n" - " #include \"test.h\"\n" - " {\n" - " float f;\n" - " }\n" - "}\n"); + " #include \"test.h\"\n" + " {\n" + " float f;\n" + " }\n" + "}\n"); ASSERT_EQUALS("[test.h:5:7] -> [test.cpp:4:15]: (style) Local variable 'f' shadows outer variable [shadowVariable]\n", errout_str()); } - } void knownArgument() {