Fixed #9919 (False positive: clarifyCalculation for code: flags & ZRL_EOL_NUL ? '\0' : '\n')

This commit is contained in:
Daniel Marjamäki 2020-09-28 19:18:34 +02:00
parent 4c9db17742
commit 8395522390
2 changed files with 22 additions and 0 deletions

View File

@ -164,6 +164,19 @@ void CheckOther::clarifyCalculation()
if (tok->astOperand1()->tokType() == Token::eBitOp && tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer > 0)
continue;
// bit operation in lhs and char literals in rhs => probably no mistake
if (tok->astOperand1()->tokType() == Token::eBitOp && Token::Match(tok->astOperand2()->astOperand1(), "%char%") && Token::Match(tok->astOperand2()->astOperand2(), "%char%"))
continue;
// bitand operation in lhs with known integer value => probably no mistake
if (tok->astOperand1()->str() == "&" && tok->astOperand1()->isBinaryOp()) {
const Token *op = tok->astOperand1()->astOperand2();
if (op->isNumber())
continue;
if (op->valueType() && op->valueType()->isEnum())
continue;
}
// Is code clarified by parentheses already?
const Token *tok2 = tok->astOperand1();
for (; tok2; tok2 = tok2->next()) {

View File

@ -4271,6 +4271,15 @@ private:
check("void f(int x) { const char *p = x & 1 ? \"1\" : \"0\"; }");
ASSERT_EQUALS("", errout.str());
check("void f(int x) { return x & 1 ? '1' : '0'; }");
ASSERT_EQUALS("", errout.str());
check("void f(int x) { return x & 16 ? 1 : 0; }");
ASSERT_EQUALS("", errout.str());
check("enum {X,Y}; void f(int x) { return x & Y ? 1 : 0; }");
ASSERT_EQUALS("", errout.str());
}
void clarifyStatement() {