Skip to content

Commit a838cb6

Browse files
committed
stlFindInsert: Take care of review comments
1 parent d2d53e5 commit a838cb6

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

lib/checkstl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,13 @@ void CheckStl::checkFindInsertError(const Token *tok)
15951595
{
15961596
std::string replaceExpr;
15971597
if (tok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() && Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
1598+
if (mSettings->standards.cpp < Standards::CPP11)
1599+
// We will recommend using emplace/try_emplace instead
1600+
return;
1601+
const std::string f = (mSettings->standards.cpp < Standards::CPP17) ? "emplace" : "try_emplace";
15981602
replaceExpr = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" +
15991603
tok->astParent()->astOperand1()->astOperand1()->expressionString() +
1600-
(mSettings->standards.cpp < Standards::CPP17 ? ".insert(" : ".emplace(") +
1604+
"." + f + "(" +
16011605
tok->astParent()->astOperand1()->astOperand2()->expressionString() +
16021606
", " +
16031607
tok->expressionString() +

test/teststl.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5153,16 +5153,40 @@ class TestStl : public TestFixture {
51535153
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout.str());
51545154
}
51555155

5156-
check("void foo() {\n"
5157-
" std::map<int, int> x;\n"
5158-
" int data = 0;\n"
5159-
" for(int i=0; i<10; ++i) {\n"
5160-
" data += 123;\n"
5161-
" if(x.find(5) == x.end())\n"
5162-
" x[5] = data;\n"
5163-
" }\n"
5164-
"}");
5165-
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'.\n", errout.str());
5156+
{ // #10558
5157+
check("void foo() {\n"
5158+
" std::map<int, int> x;\n"
5159+
" int data = 0;\n"
5160+
" for(int i=0; i<10; ++i) {\n"
5161+
" data += 123;\n"
5162+
" if(x.find(5) == x.end())\n"
5163+
" x[5] = data;\n"
5164+
" }\n"
5165+
"}", false, Standards::CPP03);
5166+
ASSERT_EQUALS("", errout.str());
5167+
5168+
check("void foo() {\n"
5169+
" std::map<int, int> x;\n"
5170+
" int data = 0;\n"
5171+
" for(int i=0; i<10; ++i) {\n"
5172+
" data += 123;\n"
5173+
" if(x.find(5) == x.end())\n"
5174+
" x[5] = data;\n"
5175+
" }\n"
5176+
"}", false, Standards::CPP11);
5177+
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'.\n", errout.str());
5178+
5179+
check("void foo() {\n"
5180+
" std::map<int, int> x;\n"
5181+
" int data = 0;\n"
5182+
" for(int i=0; i<10; ++i) {\n"
5183+
" data += 123;\n"
5184+
" if(x.find(5) == x.end())\n"
5185+
" x[5] = data;\n"
5186+
" }\n"
5187+
"}");
5188+
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.try_emplace(5, data);'.\n", errout.str());
5189+
}
51665190
}
51675191

51685192
void checkKnownEmptyContainer() {

0 commit comments

Comments
 (0)