Skip to content

Fix #11238 FP knownConditionTrueFalse with constexpr? - #4341

Merged
danmar merged 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix11238
Aug 7, 2022
Merged

danmar merged 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_Fix11238

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

@pfultz2

pfultz2 commented Aug 4, 2022

Copy link
Copy Markdown
Collaborator

Just because a function is constexpr doesn't mean it will run at compile-time with only compile-time values, so this check shouldn't just be skipped in constexpr functions.

A better fix to this issue would be to extend the isConstVarExpression to return true for constexpr nullary functions. A constexpr nullary function is pretty much equivalent to a const variable in this case.

@chrchr-github

Copy link
Copy Markdown
Collaborator Author

That would work, except if (isConstVarExpression(tok, "[|(|&|+|-|*|/|%|^|>>|<<")) skips all function calls for some reason. if the |( is removed, we no longer warn for if (strlen("abc") == 3) {}.

@pfultz2

pfultz2 commented Aug 5, 2022 •

Copy link
Copy Markdown
Collaborator

Yea I think it would better to change the function to take a predicate rather than string with bool isConstVarExpression(const Token *tok, std::function<bool(const Token*)> skipPredicate = nullptr):

if (isConstVarExpression(tok, [](const Token* tok) {
  return Token::Match(tok, "[|(|&|+|-|*|/|%|^|>>|<<") &&
         !Token::simpleMatch(tok, "( )"); })
    continue;

Which lets us skip non-nullary functions.

@danmar
danmar merged commit 078a6d7 into cppcheck-opensource:main Aug 7, 2022
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