Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5088,7 +5088,7 @@ void Scope::getVariableList()
void Scope::getVariableList(const Token* start, const Token* end)
{
// Variable declared in condition: if (auto x = bar())
if (Token::Match(classDef, "if|while ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) {
if (Token::Match(classDef, "if|while|switch ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) {
checkVariable(classDef->tokAt(2), defaultAccess());
}

Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4155,6 +4155,17 @@ class TestOther : public TestFixture {
" return r;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct Item { int state; };\n"
"void foo(std::vector<Item> &items) {\n"
" for (auto &item : items) {\n"
" switch (auto &s = item.state) {\n"
" case 0: s = 1; break;\n"
" default: break;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void constParameterCallback() {
Expand Down
17 changes: 17 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(isVariableDeclarationRValueRef);
TEST_CASE(isVariableDeclarationDoesNotIdentifyCase);
TEST_CASE(isVariableDeclarationIf);
TEST_CASE(isVariableDeclarationSwitch);
TEST_CASE(isVariableStlType);
TEST_CASE(isVariablePointerToConstPointer);
TEST_CASE(isVariablePointerToVolatilePointer);
Expand Down Expand Up @@ -1234,6 +1235,22 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(y->variable());
}

void isVariableDeclarationSwitch() {
GET_SYMBOL_DB("struct Item { int state; };\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: a more reduced example should suffice here.

"void foo(std::vector<Item> &items) {\n"
" for (auto &item : items) {\n"
" switch (auto &s = item.state) {\n"
" case 0: s = 1; break;\n"
" default: break;\n"
" }\n"
" }\n"
"}\n");
const Token *s = Token::findsimplematch(tokenizer.tokens(), "s");
ASSERT(s);
ASSERT(s->varId());
ASSERT(s->variable());
}

void VariableValueType1() {
GET_SYMBOL_DB("typedef uint8_t u8;\n"
"static u8 x;");
Expand Down
Loading