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); 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() {