diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ae31228c7..e17e8cbc1 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -671,9 +671,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var // variable is seen.. if (tok->varId() == var.declarationId()) { if (!membervar.empty()) { - if (!suppressErrors && Token::Match(tok, "%name% . %name% ;|%cop%") && tok->strAt(2) == membervar) + if (!suppressErrors && Token::Match(tok, "%name% . %name%") && tok->strAt(2) == membervar && Token::Match(tok->next()->astParent(), "%cop%|return|throw|?")) uninitStructMemberError(tok, tok->str() + "." + membervar); - else if (mTokenizer->isCPP() && !suppressErrors && Token::Match(tok, "%name% ;")) + else if (mTokenizer->isCPP() && !suppressErrors && Token::Match(tok, "%name%") && Token::Match(tok->astParent(), "return|throw|?")) uninitStructMemberError(tok, tok->str() + "." + membervar); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 08414732d..a9ee79dab 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3520,6 +3520,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: s.b\n", errout.str()); + checkUninitVar("struct S { int a; int b; };\n" // #9810 + "void f(void) {\n" + " struct S s;\n" + " return s.a ? 1 : 2;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: s.a\n", errout.str()); + // checkIfForWhileHead checkUninitVar("struct FRED {\n" " int a;\n"