parent
515649217f
commit
c5ab936088
|
@ -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())
|
||||
|
|
|
@ -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<int> 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<F> 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() {
|
||||
|
|
Loading…
Reference in New Issue