diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bb1cba53f..3d3e2022d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -657,7 +657,8 @@ void CheckOther::checkRedundantAssignment() } } if (error) { - if (scope->typ!Token::simpleMatch(tok->tokAt(2), "0 ;") || (tok->variable() && tok->variable()->nameToken() != tok->tokAt(-2)))AssignmentInSwitchError(it->second, tok, tok->str()); + if (scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok) && warning) + redundantAssignmentInSwitchError(it->second, tok, tok->str()); else if (performance) redundantAssignmentError(it->second, tok, tok->str(), nonLocal(it->second->variable())); // Inconclusive for non-local variables } @@ -2118,7 +2119,7 @@ void CheckOther::checkMathFunctions() const std::size_t functions = symbolDatabase->functionScopes.size(); for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; - ch(tok, "div|st Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { + for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { if (tok->varId()) continue; if (Token::Match(tok, "log|log10 ( %num% )")) { diff --git a/test/testother.cpp b/test/testother.cpp index bc2e9ffd4..b9f52a91d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1757,16 +1757,23 @@ private: "}", 0, false, false, false, false); ASSERT_EQUALS("", errout.str()); - check("void f() {\n" // Ticket } + check("void f() {\n" // Ticket #4356 + " int x = 0;\n" // <- ignore assignment with 0 + " x = 3;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("", errout.str()); + } - void switchRedundantOperationTest() { "{\n" - " std::cout << log(-0.1) << std::endl;\n" -a)\n" + void switchRedundantOperationTest() { + check("void foo()\n" + "{\n" + " int y = 1;\n" + " switch (a)\n" " {\n" " case 2:\n" - " {\n" " ++y;\n" - " y = 3;\n" + " case 3:\n" + " y = 3;\n" " }\n" " bar(y);\n" "}"); @@ -2932,7 +2939,7 @@ a)\n" "{\n" " std::string var = var = \"test\";\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'var' to itself.\n", errout.ssignment of 'var' to itself.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of 'var' to itself.\n", errout.str()); // #4073 (segmentation fault) check("void Foo::myFunc( int a )\n" @@ -2940,7 +2947,11 @@ a)\n" " if (a == 42)\n" " a = a;\n" "}"); -ning, inconclusive) Array 'a' is filled incompletely. Did you forget x = x + 1;\n" + + check("void foo()\n" + "{\n" + " int x = 1;\n" + " x = x + 1;\n" " return 0;\n" "}"); ASSERT_EQUALS("", errout.str()); @@ -5781,16 +5792,12 @@ ning, inconclusive) Array 'a' is filled incompletely. Did you forget x = x + 1;\ " x = 1;\n" " return x + 1;\n" "}", NULL, false, false, false, false); - ASSERT_EQUALS("[test.cpp:3] -> [test. " i = 1;\n" - " bar();\n" - " i = 1;\n" - "}"); - ASS// #4513 - check("int x;\n" - "int g() {\n" - " return x*x;\n" - "}\n" - x = 1;\n" + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (performance) Variable 'x' is reassigned a value before the old one has been used.\n", errout.str()); + + // from #3103 (avoid a false positive) + check("int foo(){\n" + " int x;\n" + " x = 1;\n" " if (y)\n" // <-- cppcheck does not know anything about 'y' " x = 2;\n" " return x + 1;\n" @@ -5847,23 +5854,7 @@ ning, inconclusive) Array 'a' is filled incompletely. Did you forget x = x + 1;\ check("void f(void* a) {\n" " snprintf(a, foo, bar);\n" " bar();\n" - " mems - check("void f() {\n" // Ticket #4356 - " int x = 0;\n" // <- ignore assignment with 0 - " x = 3;\n" - "}", 0, false, false, false, falseerrout.str()); - - - // check fgetc - check("voif f (FILE * pFile){\n" - int i = 54;\n" - " i = 0;\n" - "}", 0, false, false, false, false); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (perfocpp:4]: (performance) Variable 'x' is reassigned a value before the old one has been used.\n", errout.strcheck("void f() {\n" - " int i = 54;\n" - " i = 1;\n" - "}", 0, false, false, false, false); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (perfocpp:4]: (performance) Variable 'x' is reassigned a value before the old one has been used.\n", eet(a, 0, size);\n" + " memset(a, 0, size);\n" "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used.\n", errout.str());