Fixed #1729 (False positive: variable is assigned a value that is never used (pointer aliasing))

This commit is contained in:
Daniel Marjamäki 2010-07-07 08:26:24 +02:00
parent b0f1d5669b
commit a107cd1b59
2 changed files with 36 additions and 17 deletions

View File

@ -1382,6 +1382,20 @@ void CheckOther::functionVariableUsage()
} }
else else
variables.write(varid1); variables.write(varid1);
// pointer alias
if (var &&
var->_type == Variables::pointer &&
Token::Match(tok->previous(), "= %var% ;"))
{
const unsigned int varid2 = tok->varId();
Variables::VariableUsage *var2 = variables.find(varid2);
if (var2 && var2->_type == Variables::array)
{
variables.read(varid2);
variables.write(varid2);
}
}
} }
const Token *equal = tok->next(); const Token *equal = tok->next();

View File

@ -1231,8 +1231,7 @@ private:
" int a[10];\n" " int a[10];\n"
" int *b = a;\n" " int *b = a;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n" ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1348,7 +1347,8 @@ private:
" int *b = a;\n" " int *b = a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1503,7 +1503,8 @@ private:
" int *c = b;\n" " int *c = b;\n"
" *c = 0;\n" " *c = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1513,9 +1514,10 @@ private:
" int *d = b;\n" " int *d = b;\n"
" *d = 0;\n" " *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n" TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n" "[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); "[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1525,8 +1527,9 @@ private:
" c = b;\n" " c = b;\n"
" *c = 0;\n" " *c = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n" TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str()); "[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1538,8 +1541,9 @@ private:
" c = a;\n" " c = a;\n"
" *c = 0;\n" " *c = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n" TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str()); "[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1646,8 +1650,8 @@ private:
" d = c;\n" " d = c;\n"
" *d = 0;\n" " *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: b\n" TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
"[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("int a[10];\n" functionVariableUsage("int a[10];\n"
"void foo()\n" "void foo()\n"
@ -1659,8 +1663,9 @@ private:
" d = a; *d = 0;\n" " d = a; *d = 0;\n"
" d = c; *d = 0;\n" " d = c; *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n" TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); "[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("", errout.str());
} }
void localvaralias2() // ticket 1637 void localvaralias2() // ticket 1637
@ -1773,7 +1778,7 @@ private:
" }\n" " }\n"
" b(srcdata);\n" " b(srcdata);\n"
"}"); "}");
TODO_ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1788,7 +1793,7 @@ private:
" }\n" " }\n"
" b(srcdata);\n" " b(srcdata);\n"
"}"); "}");
TODO_ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvaralias7() // ticket 1732 void localvaralias7() // ticket 1732