Skip to content

Commit 8c2ed4e

Browse files
authored
fix #14802: false positive: shadowFunction (shadowing non-static in static) (cppcheck-opensource#8605)
1 parent cb7c24d commit 8c2ed4e

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,6 +4194,9 @@ void CheckOtherImpl::checkShadowVariables()
41944194
(functionScope->function->isStatic() || functionScope->function->isFriend()) &&
41954195
shadowed->variable() && !shadowed->variable()->isLocal())
41964196
return;
4197+
if (functionScope->functionOf && functionScope->functionOf->isClassOrStructOrUnion() && functionScope->function &&
4198+
functionScope->function->isStatic() && shadowed->function() && !shadowed->function()->isStatic())
4199+
return;
41974200
if (var.scope() && var.scope()->function && var.scope()->function->isConstructor()) {
41984201
if (shadowed->variable() && shadowed->variable()->isMember())
41994202
return;

lib/token.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_l
614614
return false; // shortcut
615615
const char *current = pattern;
616616
const char *end = pattern + pattern_len;
617-
// cppcheck-suppress shadowFunction - TODO: fix this
618617
const char *next = static_cast<const char*>(std::memchr(pattern, ' ', pattern_len));
619618
if (!next)
620619
next = end;
@@ -769,7 +768,6 @@ nonneg int Token::getStrArraySize(const Token *tok)
769768
{
770769
assert(tok != nullptr);
771770
assert(tok->tokType() == eString);
772-
// cppcheck-suppress shadowFunction - TODO: fix this
773771
const std::string str(getStringLiteral(tok->str()));
774772
int sizeofstring = 1;
775773
for (int i = 0; i < static_cast<int>(str.size()); i++) {
@@ -2359,11 +2357,9 @@ const ::Type* Token::typeOf(const Token* tok, const Token** typeTok)
23592357
if (tok->valueType() && tok->valueType()->typeScope && tok->valueType()->typeScope->definedType)
23602358
return tok->valueType()->typeScope->definedType;
23612359
if (Token::simpleMatch(tok, "return")) {
2362-
// cppcheck-suppress shadowFunction - TODO: fix this
23632360
const Scope *scope = tok->scope();
23642361
if (!scope)
23652362
return nullptr;
2366-
// cppcheck-suppress shadowFunction - TODO: fix this
23672363
const Function *function = scope->function;
23682364
if (!function)
23692365
return nullptr;
@@ -2473,18 +2469,15 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
24732469
return {var->typeStartToken(), var->typeEndToken()->next()};
24742470
}
24752471
if (Token::simpleMatch(tok, "return")) {
2476-
// cppcheck-suppress shadowFunction - TODO: fix this
24772472
const Scope* scope = tok->scope();
24782473
if (!scope)
24792474
return {};
2480-
// cppcheck-suppress shadowFunction - TODO: fix this
24812475
const Function* function = scope->function;
24822476
if (!function)
24832477
return {};
24842478
return { function->retDef, function->returnDefEnd() };
24852479
}
24862480
if (tok->previous() && tok->previous()->function()) {
2487-
// cppcheck-suppress shadowFunction - TODO: fix this
24882481
const Function *function = tok->previous()->function();
24892482
return {function->retDef, function->returnDefEnd()};
24902483
}

test/testother.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13118,6 +13118,12 @@ class TestOther : public TestFixture {
1311813118
check("struct S { int v(); explicit S(int v); };\n"
1311913119
"S::S(int v) : v(v) {}\n");
1312013120
ASSERT_EQUALS("", errout_str());
13121+
13122+
check("struct S { int i(); static void f(int i) {} };\n");
13123+
ASSERT_EQUALS("", errout_str());
13124+
13125+
check("struct S { static int i(); static void f(int i) {} };\n");
13126+
ASSERT_EQUALS("[test.cpp:1:23] -> [test.cpp:1:46]: (style) Argument 'i' shadows outer function [shadowFunction]\n", errout_str());
1312113127
}
1312213128

1312313129
void knownArgument() {

0 commit comments

Comments
 (0)