Skip to content

Commit 701d381

Browse files
Fix #11383 FP selfAssignment: lambda capture / #11380 FP operatorEqRetRefThis (cppcheck-opensource#4581)
* Fix #11383 FP selfAssignment: lambda capture / #11380 FP operatorEqRetRefThis * Format
1 parent 8aec886 commit 701d381

6 files changed

Lines changed: 34 additions & 10 deletions

File tree

lib/checkclass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,10 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
15591559

15601560
for (; tok && tok != last; tok = tok->next()) {
15611561
// check for return of reference to this
1562+
1563+
if (const Token* lScope = isLambdaCaptureList(tok)) // skip lambda
1564+
tok = lScope->link();
1565+
15621566
if (tok->str() != "return")
15631567
continue;
15641568

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,7 @@ void CheckOther::checkDuplicateExpression()
24462446
}
24472447
}
24482448
ErrorPath errorPath;
2449-
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=")) {
2449+
if (tok->isOp() && tok->astOperand1() && !Token::Match(tok, "+|*|<<|>>|+=|*=|<<=|>>=") && !isLambdaCaptureList(tok->astParent())) {
24502450
if (Token::Match(tok, "==|!=|-") && astIsFloat(tok->astOperand1(), true))
24512451
continue;
24522452
const bool pointerDereference = (tok->astOperand1() && tok->astOperand1()->isUnaryOp("*")) ||

lib/tokenlist.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,25 +1385,25 @@ static void compileExpression(Token *&tok, AST_state& state)
13851385
compileComma(tok, state);
13861386
}
13871387

1388-
static bool isLambdaCaptureList(const Token * tok)
1388+
const Token* isLambdaCaptureList(const Token * tok)
13891389
{
13901390
// a lambda expression '[x](y){}' is compiled as:
13911391
// [
13921392
// `-( <<-- optional
13931393
// `-{
13941394
// see compilePrecedence2
1395-
if (tok->str() != "[")
1396-
return false;
1395+
if (!Token::simpleMatch(tok, "["))
1396+
return nullptr;
13971397
if (!Token::Match(tok->link(), "] (|{"))
1398-
return false;
1398+
return nullptr;
13991399
if (Token::simpleMatch(tok->astOperand1(), "{") && tok->astOperand1() == tok->link()->next())
1400-
return true;
1400+
return tok->astOperand1();
14011401
if (!tok->astOperand1() || tok->astOperand1()->str() != "(")
1402-
return false;
1402+
return nullptr;
14031403
const Token * params = tok->astOperand1();
1404-
if (!params->astOperand1() || params->astOperand1()->str() != "{")
1405-
return false;
1406-
return true;
1404+
if (!Token::simpleMatch(params->astOperand1(), "{"))
1405+
return nullptr;
1406+
return params->astOperand1();
14071407
}
14081408

14091409
// Compile inner expressions inside inner ({..}) and lambda bodies

lib/tokenlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,7 @@ class CPPCHECKLIB TokenList {
221221

222222
/// @}
223223

224+
const Token* isLambdaCaptureList(const Token* tok);
225+
224226
//---------------------------------------------------------------------------
225227
#endif // tokenlistH

test/testclass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,16 @@ class TestClass : public TestFixture {
13211321
"};\n"
13221322
"A::B & A::B::operator=(const A::B & b) { return b; }");
13231323
ASSERT_EQUALS("[test.cpp:8]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
1324+
1325+
checkOpertorEqRetRefThis( // #11380
1326+
"struct S {\n"
1327+
" S& operator=(const S& other) {\n"
1328+
" i = []() { return 42; }();\n"
1329+
" return *this;\n"
1330+
" }\n"
1331+
" int i;\n"
1332+
"};\n");
1333+
ASSERT_EQUALS("", errout.str());
13241334
}
13251335

13261336
void operatorEqRetRefThis2() {

test/testother.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,6 +4850,14 @@ class TestOther : public TestFixture {
48504850
" }\n"
48514851
"};\n");
48524852
ASSERT_EQUALS("", errout.str());
4853+
4854+
check("struct S {\n" // #11383
4855+
" void f() {\n"
4856+
" auto l2 = [i = i]() { return i; };\n"
4857+
" }\n"
4858+
" int i;\n"
4859+
"};\n");
4860+
ASSERT_EQUALS("", errout.str());
48534861
}
48544862

48554863
void trac1132() {

0 commit comments

Comments
 (0)