Skip to content

Commit

Permalink
Fix #12591 FP shadowFunction ahead of function declaration (#6237)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Apr 5, 2024
1 parent 9deefe4 commit 4de6984
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3577,27 +3577,27 @@ void CheckOther::funcArgOrderDifferent(const std::string & functionName,
reportError(tokens, Severity::warning, "funcArgOrderDifferent", msg, CWE683, Certainty::normal);
}

static const Token *findShadowed(const Scope *scope, const std::string &varname, int linenr)
static const Token *findShadowed(const Scope *scope, const Variable& var, int linenr)
{
if (!scope)
return nullptr;
for (const Variable &var : scope->varlist) {
if (scope->isExecutable() && var.nameToken()->linenr() > linenr)
for (const Variable &v : scope->varlist) {
if (scope->isExecutable() && v.nameToken()->linenr() > linenr)
continue;
if (var.name() == varname)
return var.nameToken();
if (v.name() == var.name())
return v.nameToken();
}
auto it = std::find_if(scope->functionList.cbegin(), scope->functionList.cend(), [&](const Function& f) {
return f.type == Function::Type::eFunction && f.name() == varname;
return f.type == Function::Type::eFunction && f.name() == var.name() && precedes(f.tokenDef, var.nameToken());
});
if (it != scope->functionList.end())
return it->tokenDef;

if (scope->type == Scope::eLambda)
return nullptr;
const Token* shadowed = findShadowed(scope->nestedIn, varname, linenr);
const Token* shadowed = findShadowed(scope->nestedIn, var, linenr);
if (!shadowed)
shadowed = findShadowed(scope->functionOf, varname, linenr);
shadowed = findShadowed(scope->functionOf, var, linenr);
return shadowed;
}

Expand Down Expand Up @@ -3628,9 +3628,9 @@ void CheckOther::checkShadowVariables()
}
}

const Token *shadowed = findShadowed(scope.nestedIn, var.name(), var.nameToken()->linenr());
const Token *shadowed = findShadowed(scope.nestedIn, var, var.nameToken()->linenr());
if (!shadowed)
shadowed = findShadowed(scope.functionOf, var.name(), var.nameToken()->linenr());
shadowed = findShadowed(scope.functionOf, var, var.nameToken()->linenr());
if (!shadowed)
continue;
if (scope.type == Scope::eFunction && scope.className == var.name())
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11384,6 +11384,13 @@ class TestOther : public TestFixture {
" return 0;\n"
"}\n", "test.c");
ASSERT_EQUALS("[test.c:1] -> [test.c:4]: (style) Local variable 'a' shadows outer variable\n", errout_str());

check("int f() {\n" // #12591
" int g = 0;\n"
" return g;\n"
"}\n"
"int g() { return 1; }\n");
ASSERT_EQUALS("", errout_str());
}

void knownArgument() {
Expand Down

0 comments on commit 4de6984

Please sign in to comment.