diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index bcf4bf141..60f49c4a2 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -390,7 +390,7 @@ private: // Variable declaration.. if (tok.varId() && Token::Match(&tok, "%var% [[;]")) { const Variable* var2 = symbolDatabase->getVariableFromVarId(tok.varId()); - if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isConst()) { + if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isExtern() && !var2->isConst()) { if (tok.linkAt(1)) { // array const Token* endtok = tok.next(); while (endtok->link()) @@ -1044,7 +1044,7 @@ void CheckUninitVar::check() void CheckUninitVar::checkScope(const Scope* scope) { for (std::list::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { - if ((i->type() && !i->isPointer()) || i->isStatic() || i->isConst() || i->isArray() || i->isReference()) + if ((i->type() && !i->isPointer()) || i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference()) continue; if (i->nameToken()->strAt(1) == "(") continue; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index b157defe2..fd18f8e3e 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -207,6 +207,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); + checkUninitVar("void f()\n" + "{\n" + " extern int a;\n" + " a++;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar("void f()\n" "{\n" " int a;\n" @@ -1914,6 +1921,12 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("int f() {\n" + " extern int x;\n" + " return ++x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // using uninit var in condition checkUninitVar2("void f() {\n" " int x;\n"