Skip to content

Commit a8a09a7

Browse files
Fix #12900 FP useStlAlgorithm with assignment (#7247)
1 parent 06e7663 commit a8a09a7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Diff for: lib/checkstl.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,13 @@ void CheckStl::useStlAlgorithm()
29052905
return isConstExpression(tok->linkAt(-1)->astOperand2(), mSettings->library);
29062906
};
29072907

2908+
auto isAccumulation = [](const Token* tok, int varId) {
2909+
if (tok->str() != "=")
2910+
return true;
2911+
const Token* end = Token::findmatch(tok, "%varid%|;", varId); // TODO: lambdas?
2912+
return end && end->varId() != 0;
2913+
};
2914+
29082915
for (const Scope *function : mTokenizer->getSymbolDatabase()->functionScopes) {
29092916
for (const Token *tok = function->bodyStart; tok != function->bodyEnd; tok = tok->next()) {
29102917
// Parse range-based for loop
@@ -2970,8 +2977,10 @@ void CheckStl::useStlAlgorithm()
29702977
algo = "std::any_of, std::all_of, std::none_of, or std::accumulate";
29712978
else if (Token::Match(assignTok, "= %var% <|<=|>=|> %var% ? %var% : %var%") && hasVarIds(assignTok->tokAt(6), loopVar->varId(), assignVarId))
29722979
algo = minmaxCompare(assignTok->tokAt(2), loopVar->varId(), assignVarId, assignTok->tokAt(5)->varId() == assignVarId);
2973-
else
2980+
else if (isAccumulation(assignTok, assignVarId))
29742981
algo = "std::accumulate";
2982+
else
2983+
continue;
29752984
}
29762985
useStlAlgorithmError(assignTok, algo);
29772986
continue;

Diff for: test/teststl.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5430,6 +5430,14 @@ class TestStl : public TestFixture {
54305430
" return sum;\n"
54315431
"}\n");
54325432
ASSERT_EQUALS("", errout_str());
5433+
5434+
check("int f(const std::vector<int>& v) {\n" // #12900
5435+
" int x{};\n"
5436+
" for (const auto i : v)\n"
5437+
" x = dostuff(i);\n"
5438+
" return x;\n"
5439+
"}\n");
5440+
ASSERT_EQUALS("", errout_str());
54335441
}
54345442

54355443
void loopAlgoContainerInsert() {

0 commit comments

Comments
 (0)