Skip to content

Commit e071e29

Browse files
Fix #13717 FN multiCondition with NULL pointer check / #13716 FN duplicateCondition (regression) (danmar#8142)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent cfddd3e commit e071e29

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,8 +1546,8 @@ bool isUsedAsBool(const Token* const tok, const Settings& settings)
15461546
return !Token::simpleMatch(parent->astOperand1(), "dynamic_cast") && isUsedAsBool(parent, settings);
15471547
if (parent->isUnaryOp("*"))
15481548
return isUsedAsBool(parent, settings);
1549-
if (Token::Match(parent, "==|!=") && (tok->astSibling()->isNumber() || tok->astSibling()->isKeyword()) && tok->astSibling()->hasKnownIntValue() &&
1550-
tok->astSibling()->getKnownIntValue() == 0)
1549+
if (Token::Match(parent, "==|!=") && tok->valueType() && tok->valueType()->pointer &&
1550+
tok->astSibling()->hasKnownIntValue() && tok->astSibling()->getKnownIntValue() == 0)
15511551
return true;
15521552
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))
15531553
return true;

test/testcondition.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,6 +3640,24 @@ class TestCondition : public TestFixture {
36403640
"}");
36413641
ASSERT_EQUALS("[test.cpp:2:16] -> [test.cpp:3:9]: (warning) Identical condition 'handle!=nullptr', second condition is always false [identicalConditionAfterEarlyExit]\n", errout_str());
36423642

3643+
check("void f(const char* p) {\n" // #13716
3644+
" if (!p) {}\n"
3645+
" if (p == NULL) {}\n"
3646+
" if (p == nullptr) {}\n"
3647+
" if (p == 0) {}\n"
3648+
"}");
3649+
ASSERT_EQUALS("[test.cpp:2:9] -> [test.cpp:3:11]: (style) The if condition is the same as the previous if condition [duplicateCondition]\n",
3650+
errout_str());
3651+
3652+
check("int f(const char* p) {\n" // #13717
3653+
" if (p) {}\n"
3654+
" else if (!p) {}\n"
3655+
" else if (p == NULL) {}\n"
3656+
"}");
3657+
ASSERT_EQUALS("[test.cpp:2:9] -> [test.cpp:3:14]: (style) Expression is always true because 'else if' condition is opposite to previous condition at line 2. [multiCondition]\n"
3658+
"[test.cpp:4:16]: (style) Expression is always false because 'else if' condition matches previous condition at line 3. [multiCondition]\n",
3659+
errout_str());
3660+
36433661
check("void f(void* x, void* y) {\n"
36443662
" if (x == nullptr && y == nullptr)\n"
36453663
" return;\n"

0 commit comments

Comments
 (0)