diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index e6f676e7a20..a666c15b536 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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()); } diff --git a/test/testother.cpp b/test/testother.cpp index f69c7cae1c2..b12e26e1ffa 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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 &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() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 1ce349cf969..080142532d3 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -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); @@ -1234,6 +1235,22 @@ class TestSymbolDatabase : public TestFixture { ASSERT(y->variable()); } + void isVariableDeclarationSwitch() { + GET_SYMBOL_DB("struct Item { int state; };\n" + "void foo(std::vector &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;");