diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3b26385c6cd..8401d918ecc 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1254,10 +1254,12 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us } if (ftok->function()) { const std::list &argvars = ftok->function()->argumentList; - const Variable *argvar = ftok->function()->getArgumentVar(argn); - if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable &other) { - return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType()); - })) return false; + if (const Variable* argvar = ftok->function()->getArgumentVar(argn)) { + if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable& other) { + return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType()); + })) + return false; + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index cc4406a8a7e..6fd52f2e06c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1843,6 +1843,14 @@ class TestOther : public TestFixture { " printf(\"result: %d\\n\", msg);\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("void g(const char* format, ...);\n" + "void f(bool b) {\n" + " const char* s = \"abc\";\n" + " if (b)\n" + " g(\"%d %s\", 1, s);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable 's' can be reduced.\n", errout_str()); } #define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__)