diff --git a/lib/checkother.cpp b/lib/checkother.cpp index cab4cd11868..e66015c7fec 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1838,6 +1838,17 @@ static bool isCastToInteger(const Token* tok) return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0; } +static const Function* getEnclosingFunction(const Variable* var) +{ + if (var->isArgument()) + return var->scope()->function; + const Scope* scope = var->scope(); + while (scope && scope->type != ScopeType::eFunction) { + scope = scope->nestedIn; + } + return scope ? scope->function : nullptr; +} + void CheckOther::checkConstPointer() { if (!mSettings->severity.isEnabled(Severity::style) && @@ -1917,7 +1928,7 @@ void CheckOther::checkConstPointer() continue; int argn = -1; if (Token::simpleMatch(gparent, "return")) { - const Function* function = gparent->scope()->function; + const Function* function = getEnclosingFunction(var); if (function && (!Function::returnsReference(function) || Function::returnsConst(function))) continue; } diff --git a/test/testother.cpp b/test/testother.cpp index 4acbe18dfea..fadb61426a4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -13214,14 +13214,14 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); - check("struct A { bool x; };\n" + check("struct A { bool x; };\n" // #14481 "bool f(A* a) {\n" " if (a) {\n" " return a->x;\n" " }\n" " return false;\n" "}\n"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:2:11]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n", errout_str()); check("struct A { int* x; };\n" "bool f(A a) {\n"