Skip to content

Commit fd18797

Browse files
committed
Add helper
1 parent 7a1a378 commit fd18797

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

lib/astutils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,11 @@ const Token* isInLoopCondition(const Token* tok)
10221022
return Token::Match(top->previous(), "for|while (") ? top : nullptr;
10231023
}
10241024

1025+
bool isDesignatedInitializer(const Token* tok)
1026+
{
1027+
return tok && tok->isUnaryOp(".");
1028+
}
1029+
10251030
/// If tok2 comes after tok1
10261031
bool precedes(const Token * tok1, const Token * tok2)
10271032
{

lib/astutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ bool isStructuredBindingVariable(const Variable* var);
265265

266266
const Token* isInLoopCondition(const Token* tok);
267267

268+
/**
269+
* Is token the dot of a designated initializer?
270+
*/
271+
bool isDesignatedInitializer(const Token* tok);
272+
268273
/**
269274
* Is token used as boolean, that is to say cast to a bool, or used as a condition in a if/while/for
270275
*/

lib/checkautovariables.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
612612
if (tokvalue->exprId() == tok->exprId() && !(tok->variable() && tok->variable()->isArray()) &&
613613
!astIsContainerView(tok->astParent()))
614614
continue;
615-
if (tokvalue->str() == "=" && Token::simpleMatch(tokvalue->astOperand1(), ".") && !tokvalue->astOperand1()->astOperand2())
616-
tokvalue = tokvalue->astOperand2(); // designated initializer
615+
if (tokvalue->str() == "=" && isDesignatedInitializer(tokvalue->astOperand1()))
616+
tokvalue = tokvalue->astOperand2();
617617
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
618618
isInScope(tokvalue->variable()->nameToken(), scope)) ||
619619
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {

0 commit comments

Comments
 (0)