From 3fdba645a60c666cd069d5191af5cd45c4d269fa Mon Sep 17 00:00:00 2001 From: gerboengels Date: Fri, 18 Nov 2022 19:44:32 +0100 Subject: [PATCH] Improve fix for #11383 FP selfAssignment: lambda capture (#4584) --- lib/checkother.cpp | 8 +++++++- test/testother.cpp | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9a8b8dd7e5b..827d8f65624 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2445,8 +2445,14 @@ void CheckOther::checkDuplicateExpression() } } } + auto isInsideLambdaCaptureList = [](const Token* tok) { + const Token* parent = tok->astParent(); + while (Token::simpleMatch(parent, ",")) + parent = parent->astParent(); + return isLambdaCaptureList(parent); + }; ErrorPath errorPath; - if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=") && !isLambdaCaptureList(tok->astParent())) { + if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=") && !isInsideLambdaCaptureList(tok)) { if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true)) continue; const bool pointerDereference = (tok->astOperand1() && tok->astOperand1()->isUnaryOp("*")) || diff --git a/test/testother.cpp b/test/testother.cpp index 071b9a08f59..8a060de4ca1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4853,7 +4853,8 @@ class TestOther : public TestFixture { check("struct S {\n" // #11383 " void f() {\n" - " auto l2 = [i = i]() { return i; };\n" + " int x = 42;" + " auto l2 = [i = i, x, y = 0]() { return i + x + y; };\n" " }\n" " int i;\n" "};\n");