Skip to content

Commit 0a11422

Browse files
Fix #12943 FP knownConditionTrueFalse with lambda in if condition (danmar#6714)
1 parent e3613c4 commit 0a11422

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/symboldatabase.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
153153
return endInitList.top().second == scope;
154154
};
155155

156+
std::stack<const Token*> inIfCondition;
157+
156158
auto addLambda = [this, &scope](const Token* tok, const Token* lambdaEndToken) -> const Token* {
157159
const Token* lambdaStartToken = lambdaEndToken->link();
158160
const Token* argStart = lambdaStartToken->astParent();
@@ -732,7 +734,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
732734
scope->checkVariable(tok->tokAt(2), AccessControl::Local, mSettings); // check for variable declaration and add it to new scope if found
733735
else if (scope->type == Scope::eCatch)
734736
scope->checkVariable(tok->tokAt(2), AccessControl::Throw, mSettings); // check for variable declaration and add it to new scope if found
735-
tok = scopeStartTok;
737+
tok = tok->next();
738+
inIfCondition.push(scopeStartTok);
736739
} else if (Token::Match(tok, "%var% {")) {
737740
endInitList.emplace(tok->linkAt(1), scope);
738741
tok = tok->next();
@@ -741,6 +744,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
741744
} else if (tok->str() == "{") {
742745
if (inInitList()) {
743746
endInitList.emplace(tok->link(), scope);
747+
} else if (!inIfCondition.empty() && tok == inIfCondition.top()) {
748+
inIfCondition.pop();
744749
} else if (isExecutableScope(tok)) {
745750
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
746751
scope->nestedList.push_back(&scopeList.back());

test/testsymboldatabase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ class TestSymbolDatabase : public TestFixture {
433433
TEST_CASE(createSymbolDatabaseFindAllScopes6);
434434
TEST_CASE(createSymbolDatabaseFindAllScopes7);
435435
TEST_CASE(createSymbolDatabaseFindAllScopes8); // #12761
436+
TEST_CASE(createSymbolDatabaseFindAllScopes9);
436437

437438
TEST_CASE(createSymbolDatabaseIncompleteVars);
438439

@@ -5849,6 +5850,15 @@ class TestSymbolDatabase : public TestFixture {
58495850
ASSERT(myst1->scope() != myst2->scope());
58505851
}
58515852

5853+
void createSymbolDatabaseFindAllScopes9() // #12943
5854+
{
5855+
GET_SYMBOL_DB("void f(int n) {\n"
5856+
" if ([](int i) { return i == 2; }(n)) {}\n"
5857+
"}\n");
5858+
ASSERT(db && db->scopeList.size() == 4);
5859+
ASSERT_EQUALS(db->scopeList.back().type, Scope::eLambda);
5860+
}
5861+
58525862
void createSymbolDatabaseIncompleteVars()
58535863
{
58545864
{

0 commit comments

Comments
 (0)