Skip to content

Commit 6943b20

Browse files
Partial fix for #15048 Inconsistent error severity for uninitvar (#8966)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 6d667c7 commit 6943b20

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎lib/checkuninitvar.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ void CheckUninitVarImpl::uninitvarError(const Token* tok, const ValueFlow::Value
15931593
const std::string& varname = ltok ? ltok->expressionString() : "x";
15941594
ErrorPath errorPath = v.errorPath;
15951595
errorPath.emplace_back(tok, "");
1596-
auto severity = v.isKnown() ? Severity::error : Severity::warning;
1596+
auto severity = v.isKnown() && !v.conditional ? Severity::error : Severity::warning;
15971597
auto certainty = v.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
15981598
if (v.subexpressions.empty()) {
15991599
reportError(std::move(errorPath),

‎test/testuninitvar.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class TestUninitVar : public TestFixture {
9595
TEST_CASE(valueFlowUninitBreak);
9696
TEST_CASE(valueFlowUninitStructMembers);
9797
TEST_CASE(valueFlowUninitForLoop);
98+
TEST_CASE(valueFlowUninitErrorPath);
9899
TEST_CASE(uninitvar_ipa);
99100
TEST_CASE(uninitvar_memberfunction);
100101
TEST_CASE(uninitvar_nonmember); // crash in ycmd test
@@ -7965,6 +7966,23 @@ class TestUninitVar : public TestFixture {
79657966
ASSERT_EQUALS("", errout_str());
79667967
}
79677968

7969+
void valueFlowUninitErrorPath()
7970+
{
7971+
setMultiline();
7972+
7973+
valueFlowUninit("void f(bool b) {\n"
7974+
" int i;\n"
7975+
" if (b)\n"
7976+
" return i;\n"
7977+
" i = 1;\n"
7978+
" return i;\n"
7979+
"}\n");
7980+
ASSERT_EQUALS("[test.cpp:4:16]: warning: Uninitialized variable: i [uninitvar]\n"
7981+
"[test.cpp:3:9]: note: Assuming condition is true\n"
7982+
"[test.cpp:4:16]: note: Uninitialized variable: i\n",
7983+
errout_str());
7984+
}
7985+
79687986
void uninitvar_memberfunction() {
79697987
// # 8715
79707988
valueFlowUninit("struct C {\n"

0 commit comments

Comments
 (0)