Fix issue 10226: FP: redundant condition or invalid iterator (#3195)
This commit is contained in:
parent
93873be81a
commit
e0f9627201
|
@ -4476,6 +4476,7 @@ struct ConditionHandler {
|
|||
// start token of conditional code
|
||||
Token* startTokens[] = {nullptr, nullptr};
|
||||
|
||||
bool inverted = false;
|
||||
// if astParent is "!" we need to invert codeblock
|
||||
{
|
||||
const Token* tok2 = tok;
|
||||
|
@ -4484,6 +4485,7 @@ struct ConditionHandler {
|
|||
while (parent && parent->str() == "&&")
|
||||
parent = parent->astParent();
|
||||
if (parent && (parent->str() == "!" || Token::simpleMatch(parent, "== false"))) {
|
||||
inverted = true;
|
||||
std::swap(thenValues, elseValues);
|
||||
}
|
||||
tok2 = parent;
|
||||
|
@ -4568,7 +4570,7 @@ struct ConditionHandler {
|
|||
}
|
||||
|
||||
if (dead_if || dead_else) {
|
||||
if (Token::Match(tok->astParent(), "&&|&")) {
|
||||
if (!inverted && Token::Match(tok->astParent(), "&&|&")) {
|
||||
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
|
||||
changeKnownToPossible(values);
|
||||
} else {
|
||||
|
|
|
@ -3805,6 +3805,15 @@ private:
|
|||
" }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("int f(std::vector<int>::iterator it, const std::vector<int>& vector) {\n"
|
||||
" if (!(it != vector.end() && it != vector.begin()))\n"
|
||||
" throw std::out_of_range();\n"
|
||||
" if (it != vector.end() && *it == 0)\n"
|
||||
" return -1;\n"
|
||||
" return *it;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void dereferenceInvalidIterator2() {
|
||||
|
|
Loading…
Reference in New Issue