Added support for std::unique and std::remove_if to CheckStl::uselessCalls().

This commit is contained in:
PKEuS 2012-09-07 14:23:32 +02:00
parent a4b5824dec
commit e87ebcc602
3 changed files with 12 additions and 8 deletions

View File

@ -1345,8 +1345,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 (") && tok->tokAt(5)->nextArgument())
uselessCallsRemoveError(tok->next());
else if (Token::Match(tok, "[{};] std :: remove|remove_if|unique (") && tok->tokAt(5)->nextArgument())
uselessCallsRemoveError(tok->next(), tok->strAt(3));
}
}
@ -1385,9 +1385,9 @@ 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)
void CheckStl::uselessCallsRemoveError(const Token *tok, const std::string& function)
{
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. "
reportError(tok, Severity::warning, "uselessCallsRemove", "Return value of std::" + function + "() ignored. Elements remain in container.\n"
"The return value of std::" + function + "() 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.");
}

View File

@ -175,7 +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 uselessCallsRemoveError(const Token *tok, const std::string& function);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckStl c(0, settings, errorLogger);
@ -202,7 +202,7 @@ private:
c.uselessCallsSwapError(0, "str");
c.uselessCallsSubstrError(0, false);
c.uselessCallsEmptyError(0);
c.uselessCallsRemoveError(0);
c.uselessCallsRemoveError(0, "remove");
}
static std::string myName() {

View File

@ -1959,11 +1959,15 @@ private:
check("void f() {\n"
" std::remove(a.begin(), a.end(), val);\n"
" std::remove_if(a.begin(), a.end(), val);\n"
" std::unique(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"
" std::remove(\"foo.txt\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of std::remove() ignored. Elements remain in container.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of std::remove() ignored. Elements remain in container.\n"
"[test.cpp:3]: (warning) Return value of std::remove_if() ignored. Elements remain in container.\n"
"[test.cpp:4]: (warning) Return value of std::unique() ignored. Elements remain in container.\n", errout.str());
}
};