Skip to content

Commit eb4b652

Browse files
authored
Fix #15067: False positive: unreadVariable with overloaded subscript operator. (#8895)
1 parent 306f1a2 commit eb4b652

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎lib/checkunusedvar.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,11 @@ void CheckUnusedVarImpl::checkFunctionVariableUsage()
12881288
while (Token::Match(op1tok, ".|[|*"))
12891289
op1tok = op1tok->astOperand1();
12901290

1291+
// Bail out for overloaded indexing
1292+
if (op1tok && op1tok->valueType() && op1tok->valueType()->pointer == 0 &&
1293+
op1tok->valueType()->type == ValueType::Type::RECORD && Token::simpleMatch(op1tok->astParent(), "["))
1294+
continue;
1295+
12911296
// Assignment in macro => do not warn
12921297
if (isAssignment && tok->isExpandedMacro() && op1tok && op1tok->isExpandedMacro())
12931298
continue;

‎test/testunusedvar.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class TestUnusedVar : public TestFixture {
225225
TEST_CASE(localvarStruct13); // #10398
226226
TEST_CASE(localvarStruct14);
227227
TEST_CASE(localvarStructArray);
228+
TEST_CASE(localvarOverloadedSubscript);
228229
TEST_CASE(localvarUnion1);
229230

230231
TEST_CASE(localvarOp); // Usage with arithmetic operators
@@ -5666,6 +5667,16 @@ class TestUnusedVar : public TestFixture {
56665667
ASSERT_EQUALS("[test.cpp:3:12]: (style) Variable 'x[0].a' is assigned a value that is never used. [unreadVariable]\n", errout_str());
56675668
}
56685669

5670+
void localvarOverloadedSubscript() {
5671+
functionVariableUsage("struct A { B &operator [](size_t i); }\n"
5672+
"struct B { int x; };\n"
5673+
"void f(B *b) {\n"
5674+
" A a(b);\n"
5675+
" a[0].b = 0;\n"
5676+
"}\n");
5677+
ASSERT_EQUALS("", errout_str());
5678+
}
5679+
56695680
void localvarUnion1() {
56705681
// #9707
56715682
functionVariableUsage("static short read(FILE *fp) {\n"

0 commit comments

Comments
 (0)