diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 6583bef8b..aea97e47a 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1386,6 +1386,12 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, else if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) tok = tok->linkAt(1); + else if (tok->str() == "?") + // TODO: False negatives when "?:" is used. + // Fix the tokenizer and then remove this bailout. + // The tokenizer should replace "return x?y:z;" with "if(x)return y;return z;" + return true; + tok = tok->next(); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 8aa1040b6..54d856b58 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2634,6 +2634,25 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("int f(int x) {\n" // FP with ?: + " int a;\n" + " if (x)\n" + " a = p;\n" + " return x ? 2*a : 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + // TODO: False negative when "?:" is used + // This should probably be fixed in the tokenizer by changing + // "return x?y:z;" to "if(x)return y;return z;" + checkUninitVar2("int f(int x) {\n" + " int a;\n" + " if (x)\n" + " a = p;\n" + " return y ? 2*a : 3*a;\n" + "}\n"); + TODO_ASSERT_EQUALS("error", "", errout.str()); + // Unknown => bail out.. checkUninitVar2("void f(int x) {\n" " int i;\n"