Skip to content

Fix redundant FP assignment with unsigned zero - #2521

Merged
danmar merged 5 commits into
cppcheck-opensource:masterfrom
rikardfalkeborn:fix-redundant-assignment-unsigned-zero
Feb 9, 2020
Merged

danmar merged 5 commits into
cppcheck-opensource:masterfrom
rikardfalkeborn:fix-redundant-assignment-unsigned-zero

Conversation

@rikardfalkeborn

@rikardfalkeborn rikardfalkeborn commented Feb 3, 2020 •

Copy link
Copy Markdown
Contributor

Technically, it's not really an FP, but I think it's inconsistent that assigning with 0 gives no warning, and 0U gives a warning.

Comment thread lib/checkother.cpp Outdated
const Token* rhs = tok->astOperand2();
// Do not warn about assignment with 0 / NULL
if (Token::simpleMatch(tok->astOperand2(), "0") || FwdAnalysis::isNullOperand(tok->astOperand2()))
if ((rhs && rhs->isNumber() && MathLib::isNullValue(rhs->str())) || isNullOperand(rhs))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this rhs->isNumber() needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we need it because isNullValue doesn't check that it's input is a number, causing for example isNullValue(-ENOMEM) to be true... I'll remove the check here and add the check to isNullValue() instead.

Comment thread lib/astutils.cpp Outdated
{
if (!expr)
return false;
if (Token::Match(expr, "( %name% %name%| * )") &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could handle c++ casts better.

Something like:

if (!expr->isCast())
    return Token::Match(expr, "NULL|nullptr");
if (expr->valueType() && expr->valueType()->pointer == 0)
    return false;
const Token *castOp = expr->astOperand2() ? expr->astOperand2() : expr->astOperand1();
return Token::Match(castOp, "NULL|nullptr") || (MathLib::isInt(castOp->str()) && MathLib::isNullValue(castOp->str()));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get isCast() true for any tokens in static_cast<int*>(0) (is this the expected outcome?) so I hade to add an explicit check for the c++ casts. Perhaps there should be a helper for this, it seems like it could be used in many places?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange. The isCast() should be true for the ( token. We can fix that separately.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange. The isCast() should be true for the ( token. We can fix that separately.

I can reproduce this. It feels like a bug. Maybe there is something I am missing.

@danmar
danmar merged commit b1c6f29 into cppcheck-opensource:master Feb 9, 2020
@rikardfalkeborn
rikardfalkeborn deleted the fix-redundant-assignment-unsigned-zero branch February 9, 2020 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants