Skip to content

Commit 0589dca

Browse files
Fix #14160 FP unreadVariable for smart pointer (danmar#8452)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 69d89c5 commit 0589dca

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,13 +1359,19 @@ 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;
1362+
if (const ValueType *vt = expr->valueType()) {
1363+
if (vt->type == ValueType::RECORD &&
1364+
!vt->pointer &&
1365+
vt->typeScope &&
1366+
vt->typeScope->definedType &&
1367+
!symbolDatabase->isRecordTypeWithoutSideEffects(vt->typeScope->definedType))
1368+
continue;
1369+
1370+
if (vt->type == ValueType::SMART_POINTER &&
1371+
vt->smartPointerType &&
1372+
!symbolDatabase->isRecordTypeWithoutSideEffects(vt->smartPointerType))
1373+
continue;
1374+
}
13691375

13701376
FwdAnalysis fwdAnalysis(*mSettings);
13711377
const Token* scopeEnd = ValueFlow::getEndOfExprScope(expr, scope, /*smallest*/ false);

test/testunusedvar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6767,6 +6767,13 @@ class TestUnusedVar : public TestFixture {
67676767
" p = nullptr;\n"
67686768
"}\n");
67696769
ASSERT_EQUALS("", errout_str());
6770+
6771+
functionVariableUsage("struct S { S(); };\n" // #14160
6772+
"void f() {\n"
6773+
" auto p = std::make_unique<S>();\n"
6774+
" auto q = std::make_unique<U>();\n"
6775+
"}\n");
6776+
ASSERT_EQUALS("", errout_str());
67706777
}
67716778

67726779
// ticket #3104 - false positive when variable is read with "if (NOT var)"

0 commit comments

Comments
 (0)