Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

linenr() is compared without the file index. A global from an included header with a higher line number is now skipped, which is a new false negative compared with base. A function in a header that shadows a later global in the .c file still warns. The same-line case still warns too. For the global-scope case, consider !precedes(v.nameToken(), var.nameToken()), which the function lookup below already uses. eNamespace scopes have the same issue: namespace N { int f(){int i=0;return i;} int i=1; } still warns.

continue;
if (v.name() == var.name())
return v.nameToken();
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading