New check: Ensure that the return value of std::remove() is used.
This commit is contained in:
parent
b641a10e35
commit
a5bca705a5
|
@ -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.");
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue