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
13 changes: 7 additions & 6 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3432,14 +3432,15 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Library&
if (!ftok)
return ExprUsage::None;
const Function* func = ftok->function();
const Variable* var = ftok->variable();
// variable init/constructor call?
if (!func && ftok->variable() && ftok == ftok->variable()->nameToken()) {
// STL types or containers don't initialize external variables
if (indirect == 0 && (ftok->variable()->isStlType() || (ftok->variable()->valueType() && ftok->variable()->valueType()->container)))
if (!func && var && ftok == var->nameToken()) {
// STL types/containers/primitive types don't initialize external variables
if (indirect == 0 && (var->isStlType() || (var->valueType() && (var->valueType()->container || (var->valueType()->isPrimitive() && !var->isReference())))))
return ExprUsage::Used;
// TODO: resolve multiple constructors
if (ftok->variable()->type() && ftok->variable()->type()->classScope) {
const int nCtor = ftok->variable()->type()->classScope->numConstructors;
if (var->type() && var->type()->classScope) {
const int nCtor = var->type()->classScope->numConstructors;
if (nCtor == 0) {
if (indirect > 0) {
const std::vector<const Variable*> argvar = getArgumentVars(ftok->astParent(), argnr);
Expand All @@ -3449,7 +3450,7 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Library&
return ExprUsage::Used;
}
if (nCtor == 1) {
const Scope* scope = ftok->variable()->type()->classScope;
const Scope* scope = var->type()->classScope;
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [](const Function& f) {
return f.isConstructor();
});
Expand Down
20 changes: 20 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6860,6 +6860,26 @@ class TestUninitVar : public TestFixture {
" return a[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("int f() {\n" // #15210
" int a;\n"
" int b{ a };\n"
" return b;\n"
"}\n"
"int g() {\n"
" int a;\n"
" int b(a);\n"
" return b;\n"
"}\n"
"int h() {\n"
" int a;\n"
" int& b{ a };\n"
" b = 0;\n"
" return b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:12]: (error) Uninitialized variable: a [uninitvar]\n"
"[test.cpp:8:11]: (error) Uninitialized variable: a [uninitvar]\n",
errout_str());
}

void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
Expand Down
Loading