MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkFindInsert

Method checkFindInsert

lib/checkstl.cpp:1686–1730  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1684}
1685
1686void CheckStlImpl::checkFindInsert()
1687{
1688 if (!mSettings.severity.isEnabled(Severity::performance))
1689 return;
1690
1691 logChecker("CheckStl::checkFindInsert"); // performance
1692
1693 const SymbolDatabase *const symbolDatabase = mTokenizer->getSymbolDatabase();
1694 for (const Scope *scope : symbolDatabase->functionScopes) {
1695 for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
1696 if (!Token::simpleMatch(tok, "if ("))
1697 continue;
1698 if (!Token::simpleMatch(tok->linkAt(1), ") {"))
1699 continue;
1700 if (!Token::Match(tok->next()->astOperand2(), "%comp%"))
1701 continue;
1702 const Token *condTok = tok->next()->astOperand2();
1703 const Token *containerTok;
1704 const Token *keyTok;
1705 std::tie(containerTok, keyTok) = isMapFind(condTok->astOperand1());
1706 if (!containerTok)
1707 continue;
1708 // In < C++17 we only warn for small simple types
1709 if (mSettings.standards.cpp < Standards::CPP17 && !(keyTok && keyTok->valueType() && (keyTok->valueType()->isIntegral() || keyTok->valueType()->pointer > 0)))
1710 continue;
1711
1712 const Token *thenTok = tok->linkAt(1)->next();
1713 const Token *valueTok = findInsertValue(thenTok, containerTok, keyTok, mSettings);
1714 if (!valueTok)
1715 continue;
1716
1717 if (Token::simpleMatch(thenTok->link(), "} else {")) {
1718 const Token *valueTok2 =
1719 findInsertValue(thenTok->link()->tokAt(2), containerTok, keyTok, mSettings);
1720 if (!valueTok2)
1721 continue;
1722 if (isSameExpression(true, valueTok, valueTok2, mSettings, true, true)) {
1723 checkFindInsertError(valueTok);
1724 }
1725 } else {
1726 checkFindInsertError(valueTok);
1727 }
1728 }
1729 }
1730}
1731
1732void CheckStlImpl::checkFindInsertError(const Token *tok)
1733{

Callers 1

runChecksMethod · 0.80

Calls 11

isMapFindFunction · 0.85
findInsertValueFunction · 0.85
isSameExpressionFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
astOperand2Method · 0.80
astOperand1Method · 0.80
isIntegralMethod · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected