Skip to content

Commit 26e0795

Browse files
committed
Format
1 parent 9b6e9f8 commit 26e0795

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

‎lib/checkstl.cpp‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "errortypes.h"
2424
#include "library.h"
2525
#include "mathlib.h"
26-
#include "refthunk.h"
2726
#include "pathanalysis.h"
27+
#include "refthunk.h"
2828
#include "settings.h"
2929
#include "standards.h"
3030
#include "symboldatabase.h"
@@ -1748,9 +1748,9 @@ void CheckStlImpl::checkFindInsert()
17481748
/** When is the condition of the 'if' statement checked by checkFindInsert() true? */
17491749
enum class FindInsertCond : std::uint8_t { UNKNOWN, FOUND, NOT_FOUND };
17501750

1751-
static FindInsertCond getFindInsertCond(const Token *condTok)
1751+
static FindInsertCond getFindInsertCond(const Token* condTok)
17521752
{
1753-
const Token *rhs = condTok->astOperand2();
1753+
const Token* rhs = condTok->astOperand2();
17541754
if (!rhs)
17551755
return FindInsertCond::UNKNOWN;
17561756
if (Token::Match(condTok->astOperand1()->astOperand1(), ". find (")) {
@@ -1774,11 +1774,12 @@ static FindInsertCond getFindInsertCond(const Token *condTok)
17741774
return found ? FindInsertCond::FOUND : FindInsertCond::NOT_FOUND;
17751775
}
17761776

1777-
void CheckStlImpl::checkFindInsertError(const Token *tok, const Token *condTok)
1777+
void CheckStlImpl::checkFindInsertError(const Token* tok, const Token* condTok)
17781778
{
17791779
std::string suggestion;
17801780
std::string method = "insertion";
1781-
if (tok && condTok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() && Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
1781+
if (tok && condTok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() &&
1782+
Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
17821783
const std::string containerExpr = tok->astParent()->astOperand1()->astOperand1()->expressionString();
17831784
const std::string keyExpr = tok->astParent()->astOperand1()->astOperand2()->expressionString();
17841785
switch (getFindInsertCond(condTok)) {
@@ -1787,16 +1788,16 @@ void CheckStlImpl::checkFindInsertError(const Token *tok, const Token *condTok)
17871788
// We will recommend using emplace/try_emplace instead
17881789
return;
17891790
const std::string f = (mSettings.standards.cpp < Standards::CPP17) ? "emplace" : "try_emplace";
1790-
suggestion = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" +
1791-
containerExpr + "." + f + "(" + keyExpr + ", " + tok->expressionString() + ");'.";
1791+
suggestion = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" + containerExpr +
1792+
"." + f + "(" + keyExpr + ", " + tok->expressionString() + ");'.";
17921793
break;
17931794
}
17941795
case FindInsertCond::FOUND:
17951796
// The key is present, so the element is modified rather than inserted; try_emplace() would not do that
17961797
method = "modification";
17971798
suggestion = " Instead of '" + tok->astParent()->expressionString() +
1798-
"' consider assigning the value through the iterator returned by '" +
1799-
containerExpr + ".find(" + keyExpr + ")'.";
1799+
"' consider assigning the value through the iterator returned by '" + containerExpr +
1800+
".find(" + keyExpr + ")'.";
18001801
break;
18011802
case FindInsertCond::UNKNOWN:
18021803
break;

‎lib/checkstl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class CPPCHECKLIB CheckStlImpl : public CheckImpl {
220220
void stlBoundariesError(const Token* tok);
221221
void if_findError(const Token* tok, bool str);
222222
/** @param condTok the comparison in the if condition, nullptr => no specific suggestion */
223-
void checkFindInsertError(const Token *tok, const Token *condTok);
223+
void checkFindInsertError(const Token* tok, const Token* condTok);
224224
void sizeError(const Token* tok);
225225
void redundantIfRemoveError(const Token* tok);
226226
void invalidContainerLoopError(const Token* tok, const Token* loopTok, ErrorPath errorPath);

‎test/teststl.cpp‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7487,45 +7487,58 @@ class TestStl : public TestFixture {
74877487
" if (m.count(1) != 0)\n"
74887488
" m[1] = true;\n"
74897489
"}\n");
7490-
ASSERT_EQUALS("[test.cpp:4:16]: (performance) Searching before modification is not necessary. Instead of 'm[1]=true' consider assigning the value through the iterator returned by 'm.find(1)'. [stlFindInsert]\n", errout_str());
7490+
ASSERT_EQUALS(
7491+
"[test.cpp:4:16]: (performance) Searching before modification is not necessary. Instead of 'm[1]=true' consider assigning the value through the iterator returned by 'm.find(1)'. [stlFindInsert]\n",
7492+
errout_str());
74917493

74927494
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
74937495
" if (m.find(x) != m.end())\n"
74947496
" m[x] = 1;\n"
74957497
"}\n");
7496-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n", errout_str());
7498+
ASSERT_EQUALS(
7499+
"[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n",
7500+
errout_str());
74977501

74987502
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
74997503
" if (m.count(x) > 0)\n"
75007504
" m[x] = 1;\n"
75017505
"}\n");
7502-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n", errout_str());
7506+
ASSERT_EQUALS(
7507+
"[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n",
7508+
errout_str());
75037509

75047510
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
75057511
" if (m.count(x) == 1)\n"
75067512
" m[x] = 1;\n"
75077513
"}\n");
7508-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n", errout_str());
7514+
ASSERT_EQUALS(
7515+
"[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n",
7516+
errout_str());
75097517

75107518
// The key is not found => try_emplace()
75117519
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
75127520
" if (m.count(x) == 0)\n"
75137521
" m[x] = 1;\n"
75147522
"}\n");
7515-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. Instead of 'm[x]=1' consider using 'm.try_emplace(x, 1);'. [stlFindInsert]\n", errout_str());
7523+
ASSERT_EQUALS(
7524+
"[test.cpp:3:16]: (performance) Searching before insertion is not necessary. Instead of 'm[x]=1' consider using 'm.try_emplace(x, 1);'. [stlFindInsert]\n",
7525+
errout_str());
75167526

75177527
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
75187528
" if (m.count(x) < 1)\n"
75197529
" m[x] = 1;\n"
75207530
"}\n");
7521-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. Instead of 'm[x]=1' consider using 'm.try_emplace(x, 1);'. [stlFindInsert]\n", errout_str());
7531+
ASSERT_EQUALS(
7532+
"[test.cpp:3:16]: (performance) Searching before insertion is not necessary. Instead of 'm[x]=1' consider using 'm.try_emplace(x, 1);'. [stlFindInsert]\n",
7533+
errout_str());
75227534

75237535
// Unknown condition => no suggestion
75247536
check("void f(std::map<unsigned, unsigned>& m, unsigned x, unsigned y) {\n"
75257537
" if (m.count(x) == y)\n"
75267538
" m[x] = 1;\n"
75277539
"}\n");
7528-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n", errout_str());
7540+
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n",
7541+
errout_str());
75297542

75307543
// Both branches set the value => no suggestion
75317544
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
@@ -7534,15 +7547,17 @@ class TestStl : public TestFixture {
75347547
" else\n"
75357548
" m[x] = 1;\n"
75367549
"}\n");
7537-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n", errout_str());
7550+
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n",
7551+
errout_str());
75387552

75397553
check("void f(std::map<unsigned, unsigned>& m, unsigned x) {\n"
75407554
" if (m.find(x) != m.end())\n"
75417555
" m[x] = 1;\n"
75427556
" else\n"
75437557
" m.emplace(x, 1);\n"
75447558
"}\n");
7545-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n", errout_str());
7559+
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n",
7560+
errout_str());
75467561

75477562
// The suggestion for a found key does not depend on the C++ standard
75487563
{
@@ -7553,10 +7568,14 @@ class TestStl : public TestFixture {
75537568
"}\n";
75547569
s.standards.cpp = Standards::CPP03;
75557570
check(code, s);
7556-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n", errout_str());
7571+
ASSERT_EQUALS(
7572+
"[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n",
7573+
errout_str());
75577574
s.standards.cpp = Standards::CPP11;
75587575
check(code, s);
7559-
ASSERT_EQUALS("[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n", errout_str());
7576+
ASSERT_EQUALS(
7577+
"[test.cpp:3:16]: (performance) Searching before modification is not necessary. Instead of 'm[x]=1' consider assigning the value through the iterator returned by 'm.find(x)'. [stlFindInsert]\n",
7578+
errout_str());
75607579
}
75617580
}
75627581

0 commit comments

Comments
 (0)