Skip to content
Merged
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
12 changes: 12 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,17 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(y->variable());
}

void isVariableDeclarationSwitch() {
GET_SYMBOL_DB("void foo(void) {\n"
" int x = 0;\n"
" switch (auto &s = x) {}\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