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
5 changes: 5 additions & 0 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,11 @@ void CheckUnusedVarImpl::checkFunctionVariableUsage()
while (Token::Match(op1tok, ".|[|*"))
op1tok = op1tok->astOperand1();

// Bail out for overloaded indexing
if (op1tok && op1tok->valueType() && op1tok->valueType()->pointer == 0 &&
op1tok->valueType()->type == ValueType::Type::RECORD && Token::simpleMatch(op1tok->astParent(), "["))
continue;

// Assignment in macro => do not warn
if (isAssignment && tok->isExpandedMacro() && op1tok && op1tok->isExpandedMacro())
continue;
Expand Down
11 changes: 11 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(localvarStruct13); // #10398
TEST_CASE(localvarStruct14);
TEST_CASE(localvarStructArray);
TEST_CASE(localvarOverloadedSubscript);
TEST_CASE(localvarUnion1);

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

void localvarOverloadedSubscript() {
functionVariableUsage("struct A { B &operator [](size_t i); }\n"
"struct B { int x; };\n"
"void f(B *b) {\n"
" A a(b);\n"
" a[0].b = 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void localvarUnion1() {
// #9707
functionVariableUsage("static short read(FILE *fp) {\n"
Expand Down
Loading