From 445bc7a0a2f40d0b5a7f5de0024c248a721c7c9c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:30:28 +0100 Subject: [PATCH] Fix #13153 FN: constStatement (Local Lambda Expression) (#7170) --- lib/checkother.cpp | 4 ++++ test/testincompletestatement.cpp | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2612e360cc1..1f6bee00756 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2042,6 +2042,8 @@ static bool isConstStatement(const Token *tok, bool isNestedBracket = false) } return isConstStatement(tok->astOperand2(), /*isNestedBracket*/ !isChained); } + if (!tok->astParent() && findLambdaEndToken(tok)) + return true; return false; } @@ -2184,6 +2186,8 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, msg = "Redundant code: Found unused member access."; else if (tok->str() == "[" && tok->tokType() == Token::Type::eExtendedOp) msg = "Redundant code: Found unused array access."; + else if (tok->str() == "[" && !tok->astParent()) + msg = "Redundant code: Found unused lambda."; else if (mSettings->debugwarnings) { reportError(tok, Severity::debug, "debug", "constStatementError not handled."); return; diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index e70cffc1480..48637912628 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -718,6 +718,13 @@ class TestIncompleteStatement : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found unused array access.\n", errout_str()); + + check("void f() {\n" // #13153 + " []() {} ();\n" + " []() {};\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant code: Found unused lambda.\n", + errout_str()); } void vardecl() {