Skip to content

Commit cb9f2a2

Browse files
authored
Fix #14712: FP constVariableReference , taking non const reference to member (#8767)
1 parent da92ba3 commit cb9f2a2

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5088,7 +5088,7 @@ void Scope::getVariableList()
50885088
void Scope::getVariableList(const Token* start, const Token* end)
50895089
{
50905090
// Variable declared in condition: if (auto x = bar())
5091-
if (Token::Match(classDef, "if|while ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) {
5091+
if (Token::Match(classDef, "if|while|switch ( %type%") && Token::simpleMatch(classDef->next()->astOperand2(), "=")) {
50925092
checkVariable(classDef->tokAt(2), defaultAccess());
50935093
}
50945094

test/testother.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4155,6 +4155,17 @@ class TestOther : public TestFixture {
41554155
" return r;\n"
41564156
"}\n");
41574157
ASSERT_EQUALS("", errout_str());
4158+
4159+
check("struct Item { int state; };\n"
4160+
"void foo(std::vector<Item> &items) {\n"
4161+
" for (auto &item : items) {\n"
4162+
" switch (auto &s = item.state) {\n"
4163+
" case 0: s = 1; break;\n"
4164+
" default: break;\n"
4165+
" }\n"
4166+
" }\n"
4167+
"}\n");
4168+
ASSERT_EQUALS("", errout_str());
41584169
}
41594170

41604171
void constParameterCallback() {

test/testsymboldatabase.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class TestSymbolDatabase : public TestFixture {
204204
TEST_CASE(isVariableDeclarationRValueRef);
205205
TEST_CASE(isVariableDeclarationDoesNotIdentifyCase);
206206
TEST_CASE(isVariableDeclarationIf);
207+
TEST_CASE(isVariableDeclarationSwitch);
207208
TEST_CASE(isVariableStlType);
208209
TEST_CASE(isVariablePointerToConstPointer);
209210
TEST_CASE(isVariablePointerToVolatilePointer);
@@ -1235,6 +1236,17 @@ class TestSymbolDatabase : public TestFixture {
12351236
ASSERT(y->variable());
12361237
}
12371238

1239+
void isVariableDeclarationSwitch() {
1240+
GET_SYMBOL_DB("void foo(void) {\n"
1241+
" int x = 0;\n"
1242+
" switch (auto &s = x) {}\n"
1243+
"}\n");
1244+
const Token *s = Token::findsimplematch(tokenizer.tokens(), "s");
1245+
ASSERT(s);
1246+
ASSERT(s->varId());
1247+
ASSERT(s->variable());
1248+
}
1249+
12381250
void VariableValueType1() {
12391251
GET_SYMBOL_DB("typedef uint8_t u8;\n"
12401252
"static u8 x;");

0 commit comments

Comments
 (0)