Skip to content

Commit bbfcccf

Browse files
committed
Refactorization: Replace several push_back-sequences by initializer lists
1 parent 6f9c115 commit bbfcccf

16 files changed

+48
-122
lines changed

test/testbufferoverrun.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,8 +3111,7 @@ class TestBufferOverrun : public TestFixture {
31113111
}
31123112

31133113
void countSprintfLength() const {
3114-
std::list<const Token*> unknownParameter;
3115-
unknownParameter.push_back(0);
3114+
std::list<const Token*> unknownParameter(1, nullptr);
31163115

31173116
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("Hello", unknownParameter));
31183117
ASSERT_EQUALS(2, CheckBufferOverrun::countSprintfLength("s", unknownParameter));
@@ -3134,8 +3133,7 @@ class TestBufferOverrun : public TestFixture {
31343133
ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("%%%%%d", unknownParameter));
31353134

31363135
Token strTok(0);
3137-
std::list<const Token*> stringAsParameter;
3138-
stringAsParameter.push_back(&strTok);
3136+
std::list<const Token*> stringAsParameter(1, &strTok);
31393137
strTok.str("\"\"");
31403138
ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("str%s", stringAsParameter));
31413139
strTok.str("\"12345\"");
@@ -3149,10 +3147,9 @@ class TestBufferOverrun : public TestFixture {
31493147
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.6s", stringAsParameter));
31503148
ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%6.6s", stringAsParameter));
31513149

3152-
std::list<const Token*> intAsParameter;
31533150
Token numTok(0);
31543151
numTok.str("12345");
3155-
intAsParameter.push_back(&numTok);
3152+
std::list<const Token*> intAsParameter(1, &numTok);
31563153
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%02ld", intAsParameter));
31573154
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%08ld", intAsParameter));
31583155
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%.2d", intAsParameter));
@@ -3165,26 +3162,21 @@ class TestBufferOverrun : public TestFixture {
31653162
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%1.5x", intAsParameter));
31663163
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.1x", intAsParameter));
31673164

3168-
std::list<const Token*> floatAsParameter;
31693165
Token floatTok(0);
31703166
floatTok.str("1.12345f");
3171-
floatAsParameter.push_back(&floatTok);
3167+
std::list<const Token*> floatAsParameter(1, &floatTok);
31723168
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
31733169
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%8.2f", floatAsParameter));
31743170
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter));
31753171

3176-
std::list<const Token*> floatAsParameter2;
31773172
Token floatTok2(0);
31783173
floatTok2.str("100.12345f");
3179-
floatAsParameter2.push_back(&floatTok2);
3174+
std::list<const Token*> floatAsParameter2(1, &floatTok2);
31803175
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter2));
31813176
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
31823177
TODO_ASSERT_EQUALS(7, 5, CheckBufferOverrun::countSprintfLength("%4.2f", floatAsParameter));
31833178

3184-
std::list<const Token*> multipleParams;
3185-
multipleParams.push_back(&strTok);
3186-
multipleParams.push_back(0);
3187-
multipleParams.push_back(&numTok);
3179+
std::list<const Token*> multipleParams = { &strTok, nullptr, &numTok };
31883180
ASSERT_EQUALS(15, CheckBufferOverrun::countSprintfLength("str%s%d%d", multipleParams));
31893181
ASSERT_EQUALS(26, CheckBufferOverrun::countSprintfLength("str%-6s%08ld%08ld", multipleParams));
31903182
}

test/testcondition.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ class TestCondition : public TestFixture {
114114
settings0.inconclusive = inconclusive;
115115

116116
// Raw tokens..
117-
std::vector<std::string> files;
118-
files.push_back(filename);
117+
std::vector<std::string> files(1, filename);
119118
std::istringstream istr(code);
120119
const simplecpp::TokenList tokens1(istr, files, files[0]);
121120

test/testerrorlogger.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ class TestErrorLogger : public TestFixture {
120120
}
121121

122122
void ErrorMessageConstructLocations() const {
123-
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
124-
locs.push_back(fooCpp5);
125-
locs.push_back(barCpp8);
123+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
126124
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false);
127125
ASSERT_EQUALS(2, (int)msg._callStack.size());
128126
ASSERT_EQUALS("Programming error.", msg.shortMessage());
@@ -142,9 +140,7 @@ class TestErrorLogger : public TestFixture {
142140
}
143141

144142
void ErrorMessageVerboseLocations() const {
145-
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
146-
locs.push_back(fooCpp5);
147-
locs.push_back(barCpp8);
143+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
148144
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
149145
ASSERT_EQUALS(2, (int)msg._callStack.size());
150146
ASSERT_EQUALS("Programming error.", msg.shortMessage());
@@ -175,9 +171,7 @@ class TestErrorLogger : public TestFixture {
175171

176172
void CustomFormatLocations() const {
177173
// Check that first location from location stack is used in template
178-
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
179-
locs.push_back(fooCpp5);
180-
locs.push_back(barCpp8);
174+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
181175
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
182176
ASSERT_EQUALS(2, (int)msg._callStack.size());
183177
ASSERT_EQUALS("Programming error.", msg.shortMessage());
@@ -202,9 +196,7 @@ class TestErrorLogger : public TestFixture {
202196
}
203197

204198
void ToXmlV2Locations() const {
205-
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
206-
locs.push_back(fooCpp5);
207-
locs.push_back(barCpp8);
199+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
208200
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
209201
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
210202
header += " <cppcheck version=\"";

test/testimportproject.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ class TestImportProject : public TestFixture {
6464

6565
void setIncludePaths1() const {
6666
ImportProject::FileSettings fs;
67-
std::list<std::string> in;
68-
in.push_back("../include");
67+
std::list<std::string> in(1, "../include");
6968
std::map<std::string, std::string, cppcheck::stricmp> variables;
7069
fs.setIncludePaths("abc/def/", in, variables);
7170
ASSERT_EQUALS(1U, fs.includePaths.size());
@@ -74,8 +73,7 @@ class TestImportProject : public TestFixture {
7473

7574
void setIncludePaths2() const {
7675
ImportProject::FileSettings fs;
77-
std::list<std::string> in;
78-
in.push_back("$(SolutionDir)other");
76+
std::list<std::string> in(1, "$(SolutionDir)other");
7977
std::map<std::string, std::string, cppcheck::stricmp> variables;
8078
variables["SolutionDir"] = "c:/abc/";
8179
fs.setIncludePaths("/home/fred", in, variables);
@@ -85,8 +83,7 @@ class TestImportProject : public TestFixture {
8583

8684
void setIncludePaths3() const { // macro names are case insensitive
8785
ImportProject::FileSettings fs;
88-
std::list<std::string> in;
89-
in.push_back("$(SOLUTIONDIR)other");
86+
std::list<std::string> in(1, "$(SOLUTIONDIR)other");
9087
std::map<std::string, std::string, cppcheck::stricmp> variables;
9188
variables["SolutionDir"] = "c:/abc/";
9289
fs.setIncludePaths("/home/fred", in, variables);

test/testincompletestatement.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class TestIncompleteStatement : public TestFixture {
3939

4040

4141
// Raw tokens..
42-
std::vector<std::string> files;
43-
files.push_back("test.cpp");
42+
std::vector<std::string> files(1, "test.cpp");
4443
std::istringstream istr(code);
4544
const simplecpp::TokenList tokens1(istr, files, files[0]);
4645

test/testmemleak.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ class TestMemleakInFunction : public TestFixture {
404404

405405
// getcode..
406406
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings2, nullptr);
407-
std::list<const Token *> callstack;
408-
callstack.push_back(0);
407+
std::list<const Token *> callstack(1, nullptr);
409408
CheckMemoryLeak::AllocType allocType, deallocType;
410409
allocType = deallocType = CheckMemoryLeak::No;
411410
Token *tokens = checkMemoryLeak.getcode(start, callstack, varId, allocType, deallocType, classfunc, 1);
@@ -6041,8 +6040,7 @@ class TestMemleakGLib : public TestFixture {
60416040
errout.str("");
60426041

60436042
std::istringstream istr(code);
6044-
std::vector<std::string> files;
6045-
files.push_back("test.cpp");
6043+
std::vector<std::string> files(1, "test.cpp");
60466044
const simplecpp::TokenList tokens1(istr, files, files[0]);
60476045

60486046
// Preprocess...

test/testnullpointer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ class TestNullPointer : public TestFixture {
136136
settings.inconclusive = false;
137137

138138
// Raw tokens..
139-
std::vector<std::string> files;
140-
files.push_back("test.cpp");
139+
std::vector<std::string> files(1, "test.cpp");
141140
std::istringstream istr(code);
142141
const simplecpp::TokenList tokens1(istr, files, files[0]);
143142

test/testother.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ class TestOther : public TestFixture {
254254
settings->experimental = false;
255255

256256
// Raw tokens..
257-
std::vector<std::string> files;
258-
files.push_back(filename);
257+
std::vector<std::string> files(1, filename);
259258
std::istringstream istr(code);
260259
const simplecpp::TokenList tokens1(istr, files, files[0]);
261260

test/testpath.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ class TestPath : public TestFixture {
9191
}
9292

9393
void getRelative() const {
94-
std::vector<std::string> basePaths;
95-
basePaths.push_back(""); // Don't crash with empty paths
96-
basePaths.push_back("C:/foo");
97-
basePaths.push_back("C:/bar/");
98-
basePaths.push_back("C:/test.cpp");
94+
std::vector<std::string> basePaths = {
95+
"", // Don't crash with empty paths
96+
"C:/foo",
97+
"C:/bar/",
98+
"C:/test.cpp"
99+
};
99100

100101
ASSERT_EQUALS("x.c", Path::getRelativePath("C:/foo/x.c", basePaths));
101102
ASSERT_EQUALS("y.c", Path::getRelativePath("C:/bar/y.c", basePaths));

test/testpathmatch.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,25 @@ class TestPathMatch : public TestFixture {
134134
}
135135

136136
void twomasklongerpath1() const {
137-
std::vector<std::string> masks;
138-
masks.push_back("src/");
139-
masks.push_back("module/");
137+
std::vector<std::string> masks = { "src/", "module/" };
140138
PathMatch match(masks);
141139
ASSERT(!match.match("project/"));
142140
}
143141

144142
void twomasklongerpath2() const {
145-
std::vector<std::string> masks;
146-
masks.push_back("src/");
147-
masks.push_back("module/");
143+
std::vector<std::string> masks = { "src/", "module/" };
148144
PathMatch match(masks);
149145
ASSERT(match.match("project/src/"));
150146
}
151147

152148
void twomasklongerpath3() const {
153-
std::vector<std::string> masks;
154-
masks.push_back("src/");
155-
masks.push_back("module/");
149+
std::vector<std::string> masks = { "src/", "module/" };
156150
PathMatch match(masks);
157151
ASSERT(match.match("project/module/"));
158152
}
159153

160154
void twomasklongerpath4() const {
161-
std::vector<std::string> masks;
162-
masks.push_back("src/");
163-
masks.push_back("module/");
155+
std::vector<std::string> masks = { "src/", "module/" };
164156
PathMatch match(masks);
165157
ASSERT(match.match("project/src/module/"));
166158
}

0 commit comments

Comments
 (0)