Skip to content
Open
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
8 changes: 8 additions & 0 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,14 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (tok->previous() && tok->previous()->variable() && tok->previous()->variable()->nameToken()->scope()->type == ScopeType::eUnion)
continue;

if (expr->valueType() &&
expr->valueType()->type == ValueType::RECORD &&
!expr->valueType()->pointer &&
expr->valueType()->typeScope &&
expr->valueType()->typeScope->definedType &&
!symbolDatabase->isRecordTypeWithoutSideEffects(expr->valueType()->typeScope->definedType))
continue;

FwdAnalysis fwdAnalysis(*mSettings);
const Token* scopeEnd = ValueFlow::getEndOfExprScope(expr, scope, /*smallest*/ false);
if (fwdAnalysis.unusedValue(expr, start, scopeEnd)) {
Expand Down
28 changes: 28 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6681,6 +6681,34 @@ class TestUnusedVar : public TestFixture {
" C c(12);\n"
"}");
ASSERT_EQUALS("", errout_str());

// #14643
functionVariableUsage("class S { S(int); };\n"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this test case we can't see if the constructor has side effects or not. so it makes sense to not warn.

if we would see that the constructor body is empty will there be some warning then?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

"void f() { S s = 0; }\n");
ASSERT_EQUALS("", errout_str());

// #10965
functionVariableUsage("class A {\n"
"public:\n"
" A();\n"
"};\n"
"extern A cb();\n"
"void f() { const A c = cb(); }\n");
ASSERT_EQUALS("", errout_str());

// #11704
functionVariableUsage("class S {\n"
"public:\n"
" S();\n"
"};\n"
"class C {\n"
" S &s();\n"
" void f() {\n"
" const S s1 = s(); // warning\n"
" const S s2; // no warning\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}

void localVarSmartPtr() {
Expand Down
Loading