From 3ef1212448aab2197ca8b58099b86fee68665c36 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 4 May 2022 13:17:53 +0200 Subject: [PATCH 1/2] FIx FN shadowVariable in static function --- 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 fccd25ca465..c03cb62e92f 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->nestedIn && functionScope->nestedIn->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 c946fa7c814..d185a8e1c2c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9697,6 +9697,14 @@ class TestOther : public TestFixture { " 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() { From 1ba111abd94381312c1f33d17c00d6e27032c8dc Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 4 May 2022 15:20:18 +0200 Subject: [PATCH 2/2] Use functionOf --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c03cb62e92f..a8b279a521e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3407,7 +3407,7 @@ void CheckOther::checkShadowVariables() continue; if (scope.type == Scope::eFunction && scope.className == var.name()) continue; - if (functionScope->nestedIn && functionScope->nestedIn->isClassOrStructOrUnion() && functionScope->function && functionScope->function->isStatic() && + 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");