diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 5063e0094..7b8a8ca57 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1157,8 +1157,11 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, *possibleInit = true; // might be a noreturn function.. - if (_tokenizer->IsScopeNoReturn(tok)) + if (_tokenizer->IsScopeNoReturn(tok)) { + if (noreturn) + *noreturn = true; return true; + } break; } @@ -1226,7 +1229,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, } std::map varValueIf; - if (!initif) { + if (!initif && !noreturnIf) { for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) { if (Token::Match(tok2, "[;{}.] %var% = - %var% ;")) varValueIf[tok2->next()->varId()] = NOT_ZERO; @@ -1256,7 +1259,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, const bool initelse = checkScopeForVariable(scope, tok->next(), var, &possibleInitElse, NULL, membervar); std::map varValueElse; - if (!initelse) { + if (!initelse && !noreturnElse) { for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) { if (Token::Match(tok2, "[;{}.] %var% = - %var% ;")) varValueElse[tok2->next()->varId()] = NOT_ZERO; @@ -1266,7 +1269,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, } - if (initelse && condVarId > 0U) + if (initelse && condVarId > 0U && !noreturnIf && !noreturnElse) variableValue[condVarId] = condVarValue; // goto the } @@ -1278,7 +1281,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, if ((initif && noreturnElse) || (initelse && noreturnIf)) return true; - if (initif || initelse || possibleInitElse) { + if ((initif || initelse || possibleInitElse) && !noreturnIf && !noreturnElse) { ++number_of_if; variableValue.insert(varValueIf.begin(), varValueIf.end()); variableValue.insert(varValueElse.begin(), varValueElse.end()); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 48bd74cd9..b8366c396 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2245,6 +2245,17 @@ private: "}", "test.c"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("int f(void) {\n" + " int a;\n" + " int i;\n" + " if (x) { noreturn(); }\n" + " else { i = 0; }\n" + " if (i==1) { a = 0; }\n" + " else { a = 1; }\n" + " return a;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // asm checkUninitVar2("void f() {\n" " int x;\n"