Skip to content

Commit 01fd500

Browse files
authored
Fix #14643, #10965, #11704: false positive: unreadVariable for constructors with unknown side effects (danmar#8420)
1 parent f4be614 commit 01fd500

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/checkunusedvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,14 @@ void CheckUnusedVar::checkFunctionVariableUsage()
13591359
if (tok->previous() && tok->previous()->variable() && tok->previous()->variable()->nameToken()->scope()->type == ScopeType::eUnion)
13601360
continue;
13611361

1362+
if (expr->valueType() &&
1363+
expr->valueType()->type == ValueType::RECORD &&
1364+
!expr->valueType()->pointer &&
1365+
expr->valueType()->typeScope &&
1366+
expr->valueType()->typeScope->definedType &&
1367+
!symbolDatabase->isRecordTypeWithoutSideEffects(expr->valueType()->typeScope->definedType))
1368+
continue;
1369+
13621370
FwdAnalysis fwdAnalysis(*mSettings);
13631371
const Token* scopeEnd = ValueFlow::getEndOfExprScope(expr, scope, /*smallest*/ false);
13641372
if (fwdAnalysis.unusedValue(expr, start, scopeEnd)) {

test/testunusedvar.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6681,6 +6681,34 @@ class TestUnusedVar : public TestFixture {
66816681
" C c(12);\n"
66826682
"}");
66836683
ASSERT_EQUALS("", errout_str());
6684+
6685+
// #14643
6686+
functionVariableUsage("class S { S(int); };\n"
6687+
"void f() { S s = 0; }\n");
6688+
ASSERT_EQUALS("", errout_str());
6689+
6690+
// #10965
6691+
functionVariableUsage("class A {\n"
6692+
"public:\n"
6693+
" A();\n"
6694+
"};\n"
6695+
"extern A cb();\n"
6696+
"void f() { const A c = cb(); }\n");
6697+
ASSERT_EQUALS("", errout_str());
6698+
6699+
// #11704
6700+
functionVariableUsage("class S {\n"
6701+
"public:\n"
6702+
" S();\n"
6703+
"};\n"
6704+
"class C {\n"
6705+
" S &s();\n"
6706+
" void f() {\n"
6707+
" const S s1 = s(); // warning\n"
6708+
" const S s2; // no warning\n"
6709+
" }\n"
6710+
"};\n");
6711+
ASSERT_EQUALS("", errout_str());
66846712
}
66856713

66866714
void localVarSmartPtr() {

0 commit comments

Comments
 (0)