Skip to content

ValueFlow: Evaluate if statement for function returns#4908

Merged
firewave merged 14 commits intocppcheck-opensource:mainfrom
pfultz2:valueflow-return-function-eval
Mar 24, 2023
Merged

ValueFlow: Evaluate if statement for function returns#4908
firewave merged 14 commits intocppcheck-opensource:mainfrom
pfultz2:valueflow-return-function-eval

Conversation

@pfultz2
Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 commented Mar 21, 2023

No description provided.

Comment thread lib/programmemory.cpp Outdated
{
if (v.isImpossible()) {
return v.intvalue == 0;
} else {
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.

We don't have readability-else-after-return enabled, but it's still redundant.

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 did not enable that because it's just too many changes and it doesn't really improve things much. In some cases it even breaks the if-else flow of some bugger chains since some scopes return and others don't which makes it feel "wrong".

Comment thread lib/programmemory.cpp
return lhs;
return execute(expr->astOperand2(), pm);
else if (isTrue(lhs))
return execute(expr->astOperand2(), pm);
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.

No else after return

Copy link
Copy Markdown
Collaborator

@chrchr-github chrchr-github left a comment

Choose a reason for hiding this comment

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

Comment thread lib/cppcheck.cpp
checkUnusedFunctions.parseTokens(tokenizer, filename.c_str(), &mSettings);

// handling of "simple" rules has been removed.
// cppcheck-suppress 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.

Is this a FP?

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.

The implementation is disabled and it simply returns false when HAVE_RULES is not set. Depending on the case it could be considered a false positive. In this case I think it is fine and we could even move the whole method into the conditional code. The function is also private - would the function be protected or public it would be a different story as it would break the API if we make the function conditional.

I will prepare a PR adjusting these.

I think we had similar warnings with caseInsensitiveFilesystem() in path.cpp at some point. In that case I would consider it a false positive. But that looks like a case where it should use a constexpr instead of a function and thus it would not be an issue anymore.

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.

We may want to improve diagnostics in the future when its based on function call with #if conditionals in it(I believe we had a similar issue in the past). Its not trivial to implement and would make this PR much larger, so I think it would be better to merge this and then we can address this case in the future.

@firewave firewave merged commit fd8a7b9 into cppcheck-opensource:main Mar 24, 2023
@pfultz2 pfultz2 deleted the valueflow-return-function-eval branch March 24, 2023 14:54
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