From 9a379925cd2dd4549a9192901072a436a5c7d2e5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 4 May 2022 17:56:16 +0200 Subject: [PATCH] FIx FN shadowVariable in static function (#4079) * FIx FN shadowVariable in static function * Use functionOf --- lib/checkother.cpp | 3 ++- test/testother.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fccd25ca4..a8b279a52 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3407,7 +3407,8 @@ void CheckOther::checkShadowVariables() continue; if (scope.type == Scope::eFunction && scope.className == var.name()) continue; - if (functionScope->function && functionScope->function->isStatic() && shadowed->variable() && !shadowed->variable()->isLocal()) + if (functionScope->functionOf && functionScope->functionOf->isClassOrStructOrUnion() && functionScope->function && functionScope->function->isStatic() && + shadowed->variable() && !shadowed->variable()->isLocal()) continue; shadowError(var.nameToken(), shadowed, (shadowed->varId() != 0) ? "variable" : "function"); } diff --git a/test/testother.cpp b/test/testother.cpp index 21da5c39d..bf31fcf28 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9712,6 +9712,14 @@ private: " 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()); + + check("extern int a;\n" + "int a;\n" + "static int f(void) {\n" + " int a;\n" + " return 0;\n" + "}\n", "test.c"); + ASSERT_EQUALS("[test.c:1] -> [test.c:4]: (style) Local variable 'a' shadows outer variable\n", errout.str()); } void knownArgument() {