Skip to content

Fix FP knowConditionTrueFalse - #4419

Merged
orbitcowboy merged 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix10426
Aug 30, 2022
Merged

orbitcowboy merged 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix10426

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread lib/checkcondition.cpp Outdated
(Token::Match(tok->astParent(), "%oror%|&&") || Token::Match(tok->astParent()->astOperand1(), "if|while"));
const bool constValExpr = tok->isNumber() && Token::Match(tok->astParent(),"%oror%|&&|?"); // just one number in boolean expression
const bool compExpr = Token::Match(tok, "%comp%|!|("); // a compare expression
const bool compExpr = Token::Match(tok, "%comp%|!|(") && !((tok->previous() && tok->previous()->function()) || tok->isCast() || isCPPCast(tok)); // a compare expression

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.

This variable is for a compare expression, it shouldn't have ( in the match. It would be better to have another variable like callExpr to check for function call expressions: callExpr = Token::simpleMatch(tok, "(") && astIsBool(tok) && !tok->isCast(). We probably only want to warn on functions that return bools.

It seems strange to skip user-defined function calls.

@chrchr-github chrchr-github Aug 29, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It seems strange to skip user-defined function calls.

The fact that a function returns a known value might be seen as an implementation detail. I'd call this a FP:
http://cppcheck1.osuosl.org:8000/bali-phy
BAli-Phy-3.6.1/src/sequence/alphabet.cc:326:10: style: Condition 'T()' is always true [knownConditionTrueFalse]

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.

I thought isConstVarExpression would catch functions like that(from #4341)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This variable is for a compare expression, it shouldn't have ( in the match.

! is not really a compare expression either, see the inconsistencies reported in #10426. We also might want to distinguish between return expressions (warn for return type bool only) and actual conditions (warning for e.g. int is ok).

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.

Yea this whole thing needs a refactoring.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Regarding known function return values, I just saw this in daca: static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }
Warnings for this are probably not desirable. Also, there are functions that have #ifdefs inside.

@orbitcowboy
orbitcowboy merged commit 3925a27 into cppcheck-opensource:main Aug 30, 2022
@chrchr-github
chrchr-github deleted the chr_Fix10426 branch October 22, 2022 09:45
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.

3 participants