alwaysTrueFalse: Dont warn when checking sizeof for some type. such condition might be platform dependent.
This commit is contained in:
parent
39d55a6f6e
commit
55cd69e290
|
@ -1049,6 +1049,29 @@ void CheckCondition::alwaysTrueFalse()
|
|||
if (isExpandedMacro)
|
||||
continue;
|
||||
|
||||
// don't warn when condition checks sizeof result
|
||||
bool hasSizeof = false;
|
||||
tokens.push(tok);
|
||||
while (!tokens.empty()) {
|
||||
const Token *tok2 = tokens.top();
|
||||
tokens.pop();
|
||||
if (!tok2)
|
||||
continue;
|
||||
if (tok2->isNumber())
|
||||
continue;
|
||||
if (Token::simpleMatch(tok2->previous(), "sizeof (")) {
|
||||
hasSizeof = true;
|
||||
continue;
|
||||
}
|
||||
if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) {
|
||||
tokens.push(tok2->astOperand1());
|
||||
tokens.push(tok2->astOperand2());
|
||||
} else
|
||||
break;
|
||||
}
|
||||
if (tokens.empty() && hasSizeof)
|
||||
continue;
|
||||
|
||||
alwaysTrueFalseError(tok, tok->values().front().intvalue != 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1810,6 +1810,18 @@ private:
|
|||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// Avoid FP for sizeof condition
|
||||
check("void f() {\n"
|
||||
" if (sizeof(char) != 123) {}\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" int x = 123;\n"
|
||||
" if (sizeof(char) != x) {}\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'sizeof(char)!=x' 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
|
||||
check("void f() {\n"
|
||||
|
|
Loading…
Reference in New Issue