Fix false positive with inner conditions when using pointers (#1195)

This commit is contained in:
Paul Fultz II 2018-04-28 11:56:13 -05:00 committed by Daniel Marjamäki
parent 521e3495b7
commit e571e598b6
2 changed files with 28 additions and 1 deletions

View File

@ -645,7 +645,7 @@ void CheckCondition::multiCondition2()
const Token *parent = tok;
while (Token::Match(parent->astParent(), ".|[") || (Token::simpleMatch(parent->astParent(), "*") && !parent->astParent()->astOperand2()))
parent = parent->astParent();
if (Token::Match(parent->astParent(), "%assign%"))
if (Token::Match(parent->astParent(), "%assign%|++|--"))
break;
}
if (_tokenizer->isCPP() && Token::Match(tok, "%name% <<") && (!tok->valueType() || !tok->valueType()->isIntegral()))

View File

@ -90,6 +90,7 @@ private:
TEST_CASE(identicalInnerCondition);
TEST_CASE(identicalConditionAfterEarlyExit);
TEST_CASE(innerConditionModified);
TEST_CASE(clarifyCondition1); // if (a = b() < 0)
TEST_CASE(clarifyCondition2); // if (a & b == c)
@ -1989,6 +1990,32 @@ private:
ASSERT_EQUALS("", errout.str());
}
void innerConditionModified() {
check("void f(int x, int y) {\n"
" if (x == 0) {\n"
" x += y;\n"
" if (x == 0) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (x == 0) {\n"
" x += y;\n"
" if (x == 1) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int * x, int * y) {\n"
" if (x[*y] == 0) {\n"
" (*y)++;\n"
" if (x[*y] == 0) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// clarify conditions with = and comparison
void clarifyCondition1() {
check("void f() {\n"