diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 13e8bd739..ef9cdc699 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1235,6 +1235,10 @@ void CheckUninitVar::valueFlowUninit() if (!scope->isExecutable()) continue; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { + if (Token::simpleMatch(tok, "sizeof (")) { + tok = tok->linkAt(1); + continue; + } if (!tok->variable() || tok->values().size() != 1U) continue; const ValueFlow::Value &v = tok->values().front(); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index c06d812c8..89cb85b25 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -91,7 +91,6 @@ private: errout.str(""); // Tokenize.. - settings.experimental = true; settings.debugwarnings = debugwarnings; Tokenizer tokenizer(&settings, this); std::istringstream istr(code); @@ -3823,18 +3822,44 @@ private: check.deadPointer(); } + void valueFlowUninit(const char code[]) { + // Clear the error buffer.. + errout.str(""); + + // Tokenize.. + settings.debugwarnings = false; + settings.experimental = false; + + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList2(); + + // Check for redundant code.. + CheckUninitVar checkuninitvar(&tokenizer, &settings, this); + checkuninitvar.valueFlowUninit(); + } + + void valueFlowUninit() { - checkUninitVar("void f() {\n" - " int x;\n" - " switch (x) {}\n" - "}"); + valueFlowUninit("void f() {\n" + " int x;\n" + " switch (x) {}\n" + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); - checkUninitVar("int f() {\n" - " int x;\n" - " init(x);\n" - " return x;\n" // TODO: inconclusive ? - "}"); + valueFlowUninit("int f() {\n" + " int x;\n" + " init(x);\n" + " return x;\n" // TODO: inconclusive ? + "}"); + ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("void f() {\n" // #8172 + " char **x;\n" + " if (2 < sizeof(*x)) {}\n" + "}"); ASSERT_EQUALS("", errout.str()); }