TestCondition: Don't warn when sizeof is involved (#2896)

This commit is contained in:
Rikard Falkeborn 2020-11-11 08:01:11 +01:00 committed by GitHub
parent 8d600fde12
commit 829c543331
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -1472,7 +1472,6 @@ void CheckCondition::alwaysTrueFalse()
// don't warn when condition checks sizeof result
bool hasSizeof = false;
bool hasNonNumber = false;
visitAstNodes(tok, [&](const Token * tok2) {
if (!tok2)
return ChildrenToVisit::none;
@ -1484,11 +1483,10 @@ void CheckCondition::alwaysTrueFalse()
}
if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) {
return ChildrenToVisit::op1_and_op2;
} else
hasNonNumber = true;
}
return ChildrenToVisit::none;
});
if (!hasNonNumber && hasSizeof)
if (hasSizeof)
continue;
alwaysTrueFalseError(tok, &tok->values().front());

View File

@ -700,6 +700,18 @@ private:
" else if ( value & (int)Value2 ) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(size_t x) {\n"
" if (x == sizeof(int)) {}\n"
" else { if (x == sizeof(long))} {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(size_t x) {\n"
" if (x == sizeof(long)) {}\n"
" else { if (x == sizeof(long long))} {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void oppositeElseIfCondition() {
@ -3002,8 +3014,8 @@ private:
" if (sizeof(char) != x) {}\n"
" if (x != sizeof(char)) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'sizeof(char)!=x' is always true\n"
"[test.cpp:4]: (style) Condition 'x!=sizeof(char)' is always true\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'sizeof(char)!=x' is always true\n"
"[test.cpp:4]: (style) Condition 'x!=sizeof(char)' is always true\n", "", errout.str());
// Don't warn in assertions. Condition is often 'always true' by intention.
// If platform,defines,etc cause an 'always false' assertion then that is not very dangerous neither