diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 3435f56d2..5793b47db 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1340,6 +1340,8 @@ void CheckStl::uselessCalls() uselessCallsSubstrError(tok, true); } else if (Token::Match(tok, "[{}:;] %var% . empty ( ) ;") && style) uselessCallsEmptyError(tok->next()); + else if (Token::Match(tok, "[{};] std :: remove (")) + uselessCallsRemoveError(tok->next()); } } @@ -1377,3 +1379,10 @@ void CheckStl::uselessCallsEmptyError(const Token *tok) { reportError(tok, Severity::warning, "uselessCallsEmpty", "Useless call of function 'empty()'. Did you intend to call 'clear()' instead?"); } + +void CheckStl::uselessCallsRemoveError(const Token *tok) +{ + reportError(tok, Severity::warning, "uselessCallsRemove", "Return value of std::remove() ignored. Elements remain in container.\n" + "The return value of std::remove() is ignored. This function returns an iterator to the end of the range containing those elements that should be kept. " + "Elements past new end remain valid but with unspecified values. Use the erase method of the container to delete them."); +} diff --git a/lib/checkstl.h b/lib/checkstl.h index b68306a74..40a81c379 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -62,7 +62,6 @@ public: checkStl.size(); checkStl.redundantCondition(); checkStl.missingComparison(); - } @@ -176,6 +175,7 @@ private: void uselessCallsSwapError(const Token *tok, const std::string &varname); void uselessCallsSubstrError(const Token *tok, bool empty); void uselessCallsEmptyError(const Token *tok); + void uselessCallsRemoveError(const Token *tok); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { CheckStl c(0, settings, errorLogger); @@ -202,6 +202,7 @@ private: c.uselessCallsSwapError(0, "str"); c.uselessCallsSubstrError(0, false); c.uselessCallsEmptyError(0); + c.uselessCallsRemoveError(0); } static std::string myName() { diff --git a/test/teststl.cpp b/test/teststl.cpp index c8800920d..aa249729f 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1925,6 +1925,13 @@ private: " std::cout << greeting.substr(0, npos) << std::endl;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " std::remove(a.begin(), a.end(), val);\n" + " x = std::remove(a.begin(), a.end(), val);\n" + " a.erase(std::remove(a.begin(), a.end(), val));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of std::remove() ignored. Elements remain in container.\n", errout.str()); } };