Skip to content

Commit 2ee920d

Browse files
committed
Fixed #10495 (False positive: unreadVariable when assigning to reference returned by method)
1 parent 3c1ae77 commit 2ee920d

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,14 @@ bool isNullOperand(const Token *expr)
26272627

26282628
bool isGlobalData(const Token *expr, bool cpp)
26292629
{
2630+
// function call that returns reference => assume global data
2631+
if (expr && expr->str() == "(" && expr->valueType() && expr->valueType()->reference != Reference::None) {
2632+
if (expr->isBinaryOp())
2633+
return true;
2634+
if (expr->astOperand1() && precedes(expr->astOperand1(), expr))
2635+
return true;
2636+
}
2637+
26302638
bool globalData = false;
26312639
bool var = false;
26322640
visitAstNodes(expr,

test/testunusedvar.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class TestUnusedVar : public TestFixture {
173173
TEST_CASE(localvarStruct9);
174174
TEST_CASE(localvarStruct10);
175175
TEST_CASE(localvarStruct11); // 10095
176+
TEST_CASE(localvarStruct12); // #10495
176177
TEST_CASE(localvarStructArray);
177178
TEST_CASE(localvarUnion1);
178179

@@ -4509,6 +4510,16 @@ class TestUnusedVar : public TestFixture {
45094510
ASSERT_EQUALS("", errout.str());
45104511
}
45114512

4513+
void localvarStruct12() { // #10495
4514+
functionVariableUsage("struct S { bool& Ref(); };\n"
4515+
"\n"
4516+
"void Set() {\n"
4517+
" S s;\n"
4518+
" s.Ref() = true;\n"
4519+
"}");
4520+
ASSERT_EQUALS("", errout.str());
4521+
}
4522+
45124523
void localvarStructArray() {
45134524
// extracttests.start: struct X {int a;};
45144525

0 commit comments

Comments
 (0)