Skip to content

Fix FP useStlAlgorithm (don't suggest std::accumulate when nothing is accumulated)#4647

Merged
danmar merged 7 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_StlAccu
Dec 18, 2022
Merged

Fix FP useStlAlgorithm (don't suggest std::accumulate when nothing is accumulated)#4647
danmar merged 7 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_StlAccu

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread lib/checkstl.cpp
else if (accumulateBoolLiteral(assignTok, assignVarId))
algo = "std::any_of, std::all_of, std::none_of, or std::accumulate";
else
else if (assignTok->str() != "=")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be moved up to the if above, so it is if (accumulateBoolLiteral(assignTok, assignVarId) || assignTok->str() == "=")

Comment thread test/teststl.cpp
" ret = 1;\n"
" return ret;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should suggest std::any_of, std::all_of, or std::none_of.

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.

I don't think so, since those algorithms short-circuit. Presumably g() needs to be called for all elements.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Then it should probably be std::any_of, std::all_of, std::none_of, or std::accumulate. Then you can use std::accumulate when g() has side effects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could use isConstExpression to detect if the condition has side effects and then only suggest when there is no side effects.

Comment thread test/teststl.cpp
" int ret = 0;\n"
" for (const auto i : v)\n"
" if (!g(i))\n"
" ret = 1;\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

will this still warn when doing ret = ret + 1 or ret = h(ret)? We should probably check that the variable appears on the RHS.

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.

We suggest std::count_if for ret = ret + 1. There is no warning for ret = h(ret) with or without this change.

@danmar danmar merged commit b1abaf8 into cppcheck-opensource:main Dec 18, 2022
@chrchr-github chrchr-github deleted the chr_StlAccu branch February 1, 2023 14:40
scott-snyder pushed a commit to scott-snyder/cppcheck that referenced this pull request Feb 12, 2023
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