diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a3d50703d..b41d09b6f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3261,7 +3261,10 @@ static const Token *findShadowed(const Scope *scope, const std::string &varname, } if (scope->type == Scope::eLambda) return nullptr; - return findShadowed(scope->nestedIn, varname, linenr); + const Token* shadowed = findShadowed(scope->nestedIn, varname, linenr); + if (!shadowed) + shadowed = findShadowed(scope->functionOf, varname, linenr); + return shadowed; } void CheckOther::checkShadowVariables() @@ -3293,6 +3296,8 @@ void CheckOther::checkShadowVariables() } const Token *shadowed = findShadowed(scope.nestedIn, var.name(), var.nameToken()->linenr()); + if (!shadowed) + shadowed = findShadowed(scope.functionOf, var.name(), var.nameToken()->linenr()); if (!shadowed) continue; if (scope.type == Scope::eFunction && scope.className == var.name()) diff --git a/test/testother.cpp b/test/testother.cpp index 239676d58..027d89599 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9451,6 +9451,29 @@ private: " }\n" "};"); ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " int i{};\n" + " void f() { int i; }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Local variable 'i' shadows outer variable\n", errout.str()); + + check("struct S {\n" + " int i{};\n" + " std::vector v;\n" + " void f() const { for (const int& i : v) {} }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Local variable 'i' shadows outer variable\n", errout.str()); + + check("struct S {\n" // #10405 + " F* f{};\n" + " std::list fl;\n" + " void S::f() const;\n" + "};\n" + "void S::f() const {\n" + " for (const F& f : fl) {}\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:7]: (style) Local variable 'f' shadows outer variable\n", errout.str()); } void knownArgument() {