Skip to content

Commit 5d7e0ae

Browse files
committed
Fixed danmar#5084 (False positive: (style) Same expression on both sides of '&&')
1 parent 6aa03ef commit 5d7e0ae

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/checkother.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3063,6 +3063,9 @@ void CheckOther::complexDuplicateExpressionCheck(const std::list<const Function*
30633063
start = tok1;
30643064
break;
30653065
}
3066+
3067+
if (tok1->isExpandedMacro())
3068+
break;
30663069
}
30673070
const Token *end = 0;
30683071
level = 0;
@@ -3077,8 +3080,13 @@ void CheckOther::complexDuplicateExpressionCheck(const std::list<const Function*
30773080
end = tok1;
30783081
break;
30793082
}
3083+
3084+
if (tok1->isExpandedMacro())
3085+
break;
30803086
}
3081-
checkExpressionRange(constFunctions, start, end, toCheck);
3087+
3088+
if (start && end)
3089+
checkExpressionRange(constFunctions, start, end, toCheck);
30823090
}
30833091
}
30843092

lib/tokenize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5957,12 +5957,14 @@ void Tokenizer::simplifyIfNotNull()
59575957
else if (Token::Match(tok, "%var% != 0")) {
59585958
deleteFrom = tok;
59595959
tok->isPointerCompare(true);
5960+
tok->isExpandedMacro(tok->isExpandedMacro() || tok->tokAt(2)->isExpandedMacro());
59605961
}
59615962

59625963
else if (Token::Match(tok, "%var% .|:: %var% != 0")) {
59635964
tok = tok->tokAt(2);
59645965
deleteFrom = tok;
59655966
tok->isPointerCompare(true);
5967+
tok->isExpandedMacro(tok->isExpandedMacro() || tok->tokAt(2)->isExpandedMacro());
59665968
}
59675969
}
59685970

test/testother.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class TestOther : public TestFixture {
126126
TEST_CASE(incorrectLogicOperator1);
127127
TEST_CASE(incorrectLogicOperator2);
128128
TEST_CASE(incorrectLogicOperator3);
129+
TEST_CASE(incorrectLogicOperator4);
129130
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
130131
TEST_CASE(incorrectLogicOp_condSwapping);
131132
TEST_CASE(sameExpression);
@@ -3982,6 +3983,13 @@ class TestOther : public TestFixture {
39823983
"[test.cpp:5]: (warning) Logical conjunction always evaluates to false: x <= 1 && x == 3.\n", errout.str());
39833984
}
39843985

3986+
void incorrectLogicOperator4() {
3987+
check("void f(int x) {\n"
3988+
" if (x && x != $0) {}\n"
3989+
"}");
3990+
ASSERT_EQUALS("", errout.str());
3991+
}
3992+
39853993
void secondAlwaysTrueFalseWhenFirstTrueError() {
39863994
check("void f(int x) {\n"
39873995
" if (x > 5 && x != 1)\n"

0 commit comments

Comments
 (0)