From a107cd1b59fd64cb100b8a315b34ae551e6b0cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 7 Jul 2010 08:26:24 +0200 Subject: [PATCH] Fixed #1729 (False positive: variable is assigned a value that is never used (pointer aliasing)) --- lib/checkother.cpp | 14 ++++++++++++++ test/testunusedvar.cpp | 39 ++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6c34b5665..6993cd19f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1382,6 +1382,20 @@ void CheckOther::functionVariableUsage() } else 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(); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 51bfd9b9a..799557e2e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1231,8 +1231,7 @@ private: " int a[10];\n" " int *b = a;\n" "}\n"); - 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()); + ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -1348,7 +1347,8 @@ private: " int *b = a;\n" " *b = 0;\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" "{\n" @@ -1503,7 +1503,8 @@ private: " int *c = b;\n" " *c = 0;\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" "{\n" @@ -1513,9 +1514,10 @@ private: " int *d = b;\n" " *d = 0;\n" "}\n"); - 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:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); + 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: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" "{\n" @@ -1525,8 +1527,9 @@ private: " c = b;\n" " *c = 0;\n" "}\n"); - 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()); + 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()); + ASSERT_EQUALS("", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -1538,8 +1541,9 @@ private: " c = a;\n" " *c = 0;\n" "}\n"); - 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()); + 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()); + ASSERT_EQUALS("", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -1646,8 +1650,8 @@ private: " d = c;\n" " *d = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: b\n" - "[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); + TODO_ASSERT_EQUALS("[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" "void foo()\n" @@ -1659,8 +1663,9 @@ private: " d = a; *d = 0;\n" " d = c; *d = 0;\n" "}\n"); - 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()); + 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()); + ASSERT_EQUALS("", errout.str()); } void localvaralias2() // ticket 1637 @@ -1773,7 +1778,7 @@ private: " }\n" " b(srcdata);\n" "}"); - TODO_ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("", errout.str()); functionVariableUsage("void foo()\n" "{\n" @@ -1788,7 +1793,7 @@ private: " }\n" " b(srcdata);\n" "}"); - TODO_ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("", errout.str()); } void localvaralias7() // ticket 1732