diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f52be7ca2..10888c035 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1457,7 +1457,7 @@ void CheckOther::checkVariableScope() bool reduce = true; bool used = false; // Don't warn about unused variables for (; tok != var->scope()->classEnd; tok = tok->next()) { - if (tok->str() == "{" && tok->scope() != tok->previous()->scope() && !tok->isExpandedMacro()) { + if (tok->str() == "{" && tok->scope() != tok->previous()->scope() && !tok->isExpandedMacro() && tok->scope()->type != Scope::eLambda) { if (used) { bool used2 = false; if (!checkInnerScope(tok, var, used2) || used2) { diff --git a/test/testother.cpp b/test/testother.cpp index 1cf51827a..6ea30c785 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -75,6 +75,7 @@ private: TEST_CASE(varScope20); // Ticket #5103 TEST_CASE(varScope21); // Ticket #5382 TEST_CASE(varScope22); // Ticket #5684 + TEST_CASE(varScope23); // Ticket #6154 TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1063,6 +1064,16 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'p' can be reduced.\n", errout.str()); } + void varScope23() { // #6154: Don't suggest to reduce scope if inner scope is a lambda + varScope("int main() {\n" + " size_t myCounter = 0;\n" + " Test myTest([&](size_t aX){\n" + " std::cout << myCounter += aX << std::endl;\n" + " });\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void checkOldStylePointerCast(const char code[]) { // Clear the error buffer.. errout.str("");