Skip to content

Commit 019c37c

Browse files
authored
TestStl: cleaned up settings usage (#7930)
1 parent 6d9e448 commit 019c37c

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

test/teststl.cpp

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class TestStl : public TestFixture {
3232
TestStl() : TestFixture("TestStl") {}
3333

3434
private:
35-
/*const*/ Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).severity(Severity::performance).library("std.cfg").build();
35+
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).severity(Severity::performance).library("std.cfg").build();
36+
const Settings settings_i = settingsBuilder(settings).certainty(Certainty::inconclusive).build();
37+
const Settings settingsCpp03 = settingsBuilder(settings).cpp(Standards::CPP03).build();
3638

3739
void run() override {
3840
mNewTemplate = true;
@@ -181,16 +183,16 @@ class TestStl : public TestFixture {
181183
struct CheckOptions
182184
{
183185
bool inconclusive = false;
184-
Standards::cppstd_t cppstandard = Standards::CPPLatest;
186+
const Settings* s = nullptr;
185187
};
186188

187189
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
188190
template<size_t size>
189191
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
190-
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).cpp(options.cppstandard).build();
192+
const Settings* s = options.s ? options.s : (options.inconclusive ? &settings_i : &settings);
191193

192194
// Tokenize..
193-
SimpleTokenizer tokenizer(settings1, *this);
195+
SimpleTokenizer tokenizer(*s, *this);
194196

195197
ASSERT_LOC(tokenizer.tokenize(code), file, line);
196198

@@ -2636,21 +2638,19 @@ class TestStl : public TestFixture {
26362638
"}\n");
26372639
ASSERT_EQUALS("", errout_str());
26382640

2639-
const auto oldSettings = settings; // TODO: get rid of this
2640-
settings.daca = true;
2641+
Settings s = settings;
2642+
s.daca = true;
26412643

26422644
check("void f() {\n"
26432645
" const char a[][5] = { \"1\", \"true\", \"on\", \"yes\" };\n"
2644-
"}\n");
2646+
"}\n", dinit(CheckOptions, $.s = &s));
26452647
ASSERT_EQUALS("", errout_str());
2646-
2647-
settings = oldSettings;
26482648
}
26492649

26502650
void negativeIndexMultiline() {
26512651
setMultiline();
2652-
const auto oldSettings = settings; // TODO: get rid of this
2653-
settings.verbose = true;
2652+
Settings s = settings;
2653+
s.verbose = true;
26542654

26552655
check("bool valid(int);\n" // #11697
26562656
"void f(int i, const std::vector<int>& v) {\n"
@@ -2660,14 +2660,12 @@ class TestStl : public TestFixture {
26602660
"}\n"
26612661
"void g(const std::vector<int>& w) {\n"
26622662
" f(-1, w);\n"
2663-
"}\n");
2663+
"}\n", dinit(CheckOptions, $.s = &s));
26642664
ASSERT_EQUALS("[test.cpp:5:9]: warning: Array index -1 is out of bounds. [negativeContainerIndex]\n"
26652665
"[test.cpp:8:8]: note: Calling function 'f', 1st argument '-1' value is -1\n"
26662666
"[test.cpp:3:9]: note: Assuming condition is false\n"
26672667
"[test.cpp:5:9]: note: Negative array index\n",
26682668
errout_str());
2669-
2670-
settings = oldSettings;
26712669
}
26722670

26732671
void erase1() {
@@ -3788,7 +3786,7 @@ class TestStl : public TestFixture {
37883786
"{\n"
37893787
" if (x.size() == 0) {}\n"
37903788
"}";
3791-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3789+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
37923790
ASSERT_EQUALS("[test.cpp:7:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
37933791
check(code);
37943792
ASSERT_EQUALS("", errout_str());
@@ -3800,7 +3798,7 @@ class TestStl : public TestFixture {
38003798
"{\n"
38013799
" if (x.size() == 0) {}\n"
38023800
"}";
3803-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3801+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38043802
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38053803
check(code);
38063804
ASSERT_EQUALS("", errout_str());
@@ -3812,7 +3810,7 @@ class TestStl : public TestFixture {
38123810
" std::list<int> x;\n"
38133811
" if (x.size() == 0) {}\n"
38143812
"}";
3815-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3813+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38163814
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38173815
check(code);
38183816
ASSERT_EQUALS("", errout_str());
@@ -3824,7 +3822,7 @@ class TestStl : public TestFixture {
38243822
" std::list<int> x;\n"
38253823
" if (0 == x.size()) {}\n"
38263824
"}";
3827-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3825+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38283826
ASSERT_EQUALS("[test.cpp:4:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38293827
check(code);
38303828
ASSERT_EQUALS("", errout_str());
@@ -3836,7 +3834,7 @@ class TestStl : public TestFixture {
38363834
" std::list<int> x;\n"
38373835
" if (x.size() != 0) {}\n"
38383836
"}";
3839-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3837+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38403838
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38413839
check(code);
38423840
ASSERT_EQUALS("", errout_str());
@@ -3848,7 +3846,7 @@ class TestStl : public TestFixture {
38483846
" std::list<int> x;\n"
38493847
" if (0 != x.size()) {}\n"
38503848
"}";
3851-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3849+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38523850
ASSERT_EQUALS("[test.cpp:4:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38533851
check(code);
38543852
ASSERT_EQUALS("", errout_str());
@@ -3860,7 +3858,7 @@ class TestStl : public TestFixture {
38603858
" std::list<int> x;\n"
38613859
" if (x.size() > 0) {}\n"
38623860
"}";
3863-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3861+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38643862
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38653863
check(code);
38663864
ASSERT_EQUALS("", errout_str());
@@ -3872,7 +3870,7 @@ class TestStl : public TestFixture {
38723870
" std::list<int> x;\n"
38733871
" if (0 < x.size()) {}\n"
38743872
"}";
3875-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3873+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38763874
ASSERT_EQUALS("[test.cpp:4:13]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38773875
check(code);
38783876
ASSERT_EQUALS("", errout_str());
@@ -3884,7 +3882,7 @@ class TestStl : public TestFixture {
38843882
" std::list<int> x;\n"
38853883
" if (x.size() >= 1) {}\n"
38863884
"}";
3887-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3885+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
38883886
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
38893887
check(code);
38903888
ASSERT_EQUALS("", errout_str());
@@ -3896,7 +3894,7 @@ class TestStl : public TestFixture {
38963894
" std::list<int> x;\n"
38973895
" if (x.size() < 1) {}\n"
38983896
"}";
3899-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3897+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39003898
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39013899
check(code);
39023900
ASSERT_EQUALS("", errout_str());
@@ -3908,7 +3906,7 @@ class TestStl : public TestFixture {
39083906
" std::list<int> x;\n"
39093907
" if (1 <= x.size()) {}\n"
39103908
"}";
3911-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3909+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39123910
ASSERT_EQUALS("[test.cpp:4:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39133911
check(code);
39143912
ASSERT_EQUALS("", errout_str());
@@ -3920,7 +3918,7 @@ class TestStl : public TestFixture {
39203918
" std::list<int> x;\n"
39213919
" if (1 > x.size()) {}\n"
39223920
"}";
3923-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3921+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39243922
ASSERT_EQUALS("[test.cpp:4:13]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39253923
check(code);
39263924
ASSERT_EQUALS("", errout_str());
@@ -3932,7 +3930,7 @@ class TestStl : public TestFixture {
39323930
" std::list<int> x;\n"
39333931
" if (x.size()) {}\n"
39343932
"}";
3935-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3933+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39363934
ASSERT_EQUALS("[test.cpp:4:9]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39373935
check(code);
39383936
ASSERT_EQUALS("", errout_str());
@@ -3944,7 +3942,7 @@ class TestStl : public TestFixture {
39443942
" std::list<int> x;\n"
39453943
" if (!x.size()) {}\n"
39463944
"}";
3947-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3945+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39483946
ASSERT_EQUALS("[test.cpp:4:10]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39493947
check(code);
39503948
ASSERT_EQUALS("", errout_str());
@@ -3963,7 +3961,7 @@ class TestStl : public TestFixture {
39633961
" std::list<int> x;\n"
39643962
" fun(!x.size());\n"
39653963
"}";
3966-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3964+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39673965
ASSERT_EQUALS("[test.cpp:4:10]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39683966
check(code);
39693967
ASSERT_EQUALS("", errout_str());
@@ -3975,7 +3973,7 @@ class TestStl : public TestFixture {
39753973
" std::list<int> x;\n"
39763974
" fun(a && x.size());\n"
39773975
"}";
3978-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
3976+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
39793977
ASSERT_EQUALS("[test.cpp:4:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", errout_str());
39803978
check(code);
39813979
ASSERT_EQUALS("", errout_str());
@@ -4012,7 +4010,7 @@ class TestStl : public TestFixture {
40124010
"{\n"
40134011
" if (f.x.size() == 0) {}\n"
40144012
"}";
4015-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
4013+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
40164014
ASSERT_EQUALS(
40174015
"[test.cpp:10:11]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n"
40184016
"[test.cpp:10:11]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", // duplicate
@@ -4035,7 +4033,7 @@ class TestStl : public TestFixture {
40354033
"int main() {\n"
40364034
" if (zzz->x.size() > 0) { }\n"
40374035
"}";
4038-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
4036+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
40394037
ASSERT_EQUALS(
40404038
"[test.cpp:10:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n"
40414039
"[test.cpp:10:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", // duplicate
@@ -4054,7 +4052,7 @@ class TestStl : public TestFixture {
40544052
" Zzz * zzz;\n"
40554053
" if (zzz->x.size() > 0) { }\n"
40564054
"}";
4057-
check(code, dinit(CheckOptions, $.cppstandard = Standards::CPP03));
4055+
check(code, dinit(CheckOptions, $.s = &settingsCpp03));
40584056
ASSERT_EQUALS(
40594057
"[test.cpp:10:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n"
40604058
"[test.cpp:10:14]: (performance) Possible inefficient checking for 'x' emptiness. [stlSize]\n", // duplicate
@@ -6801,20 +6799,26 @@ class TestStl : public TestFixture {
68016799

68026800
// #9218 - not small type => do not warn if cpp standard is < c++17
68036801
{
6802+
Settings s = settings;
68046803
const char code[] = "void f1(std::set<LargeType>& s, const LargeType& x) {\n"
68056804
" if (s.find(x) == s.end()) {\n"
68066805
" s.insert(x);\n"
68076806
" }\n"
68086807
"}\n";
6809-
check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP11));
6808+
s.standards.cpp = Standards::CPP11;
6809+
check(code, dinit(CheckOptions, $.s = &s));
68106810
ASSERT_EQUALS("", errout_str());
6811-
check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP14));
6811+
s.standards.cpp = Standards::CPP14;
6812+
check(code, dinit(CheckOptions, $.s = &s));
68126813
ASSERT_EQUALS("", errout_str());
6813-
check(code, dinit(CheckOptions, $.inconclusive = true, $.cppstandard = Standards::CPP17));
6814+
s.standards.cpp = Standards::CPP17;
6815+
check(code, dinit(CheckOptions, $.s = &s));
68146816
ASSERT_EQUALS("[test.cpp:3:18]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n", errout_str());
68156817
}
68166818

68176819
{ // #10558
6820+
Settings s = settings;
6821+
s.standards.cpp = Standards::CPP03;
68186822
check("void foo() {\n"
68196823
" std::map<int, int> x;\n"
68206824
" int data = 0;\n"
@@ -6823,9 +6827,10 @@ class TestStl : public TestFixture {
68236827
" if(x.find(5) == x.end())\n"
68246828
" x[5] = data;\n"
68256829
" }\n"
6826-
"}", dinit(CheckOptions, $.cppstandard = Standards::CPP03));
6830+
"}", dinit(CheckOptions, $.s = &s));
68276831
ASSERT_EQUALS("", errout_str());
68286832

6833+
s.standards.cpp = Standards::CPP11;
68296834
check("void foo() {\n"
68306835
" std::map<int, int> x;\n"
68316836
" int data = 0;\n"
@@ -6834,7 +6839,7 @@ class TestStl : public TestFixture {
68346839
" if(x.find(5) == x.end())\n"
68356840
" x[5] = data;\n"
68366841
" }\n"
6837-
"}", dinit(CheckOptions, $.cppstandard = Standards::CPP11));
6842+
"}", dinit(CheckOptions, $.s = &s));
68386843
ASSERT_EQUALS("[test.cpp:7:17]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'. [stlFindInsert]\n", errout_str());
68396844

68406845
check("void foo() {\n"

0 commit comments

Comments
 (0)