From 1eafe45c478f6fbb6f9c80f2d7d04fddf9180a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 24 Apr 2010 12:21:26 +0200 Subject: [PATCH] Redundant assignment: Added TODO testcase for detecting redundant assignment in inner scope --- test/testunusedvar.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index f0e3045c1..72232b376 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -73,6 +73,7 @@ private: TEST_CASE(localvar8); TEST_CASE(localvar9); // ticket #1605 TEST_CASE(localvar10); + TEST_CASE(localvar11); TEST_CASE(localvaralias); TEST_CASE(localvarasm); @@ -641,6 +642,38 @@ private: "[test.cpp:7]: (style) Unused variable: i\n", errout.str()); } + void localvar11() + { + functionVariableUsage("void foo(int x)\n" + "{\n" + " int a = 0;\n" + " if (x == 1)\n" + " {\n" + " a = 123;\n" // redundant assignment + " return;\n" + " }\n" + " x = a;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); // catch changes + TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); + + // The variable 'a' is initialized. But the initialized value is + // never used. It is only initialized for security reasons. + functionVariableUsage("void foo(int x)\n" + "{\n" + " int a = 0;\n" + " if (x == 1)\n" + " a = 123;\n" + " else if (x == 2)\n" + " a = 456;\n" + " else\n" + " return;\n" + " x = a;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + } + void localvaralias() { functionVariableUsage("void foo()\n"