Skip to content

Commit c61d2b9

Browse files
committed
danmar#5926 Dangerous iterator comparison using operator< on 'std::deque'.
std::deque features a random access iterator, so warning stlBoundaries is a false positive
1 parent 847bb44 commit c61d2b9

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/checkstl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ void CheckStl::stlBoundaries()
707707
const Scope * scope = symbolDatabase->functionScopes[i];
708708
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
709709
// Declaring iterator..
710-
if (tok->str() == "<" && Token::Match(tok->previous(), "bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset")) {
710+
if (tok->str() == "<" && Token::Match(tok->previous(), "bitset|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset")) {
711711
const std::string& container_name(tok->strAt(-1));
712712
if (tok->link())
713713
tok = tok->link();

test/teststl.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,16 +1323,18 @@ class TestStl : public TestFixture {
13231323
ASSERT_EQUALS("", errout.str());
13241324
}
13251325

1326-
1326+
template<size_t n, typename T>
1327+
size_t getArraylength( const T(&)[n]) {
1328+
return n;
1329+
}
13271330

13281331
void stlBoundaries1() {
1329-
const int STL_CONTAINER_LIST = 9;
1330-
const std::string stlCont[STL_CONTAINER_LIST] = {
1331-
"deque", "list", "set", "multiset", "map",
1332+
const std::string stlCont[] = {
1333+
"list", "set", "multiset", "map",
13321334
"multimap", "hash_map", "hash_multimap", "hash_set"
13331335
};
13341336

1335-
for (int i = 0; i < STL_CONTAINER_LIST; ++i) {
1337+
for (size_t i = 0; i < getArraylength(stlCont); ++i) {
13361338
check("void f()\n"
13371339
"{\n"
13381340
" std::" + stlCont[i] + "<int>::iterator it;\n"
@@ -1348,6 +1350,13 @@ class TestStl : public TestFixture {
13481350
" for (it = ab.begin(); ab.end() > it; ++it) {}\n"
13491351
"}");
13501352
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous iterator comparison using operator< on 'std::forward_list'.\n", errout.str());
1353+
1354+
// #5926 no FP Dangerous iterator comparison using operator< on 'std::deque'.
1355+
check("void f() {\n"
1356+
" std::deque<int>::iterator it;\n"
1357+
" for (it = ab.begin(); ab.end() > it; ++it) {}\n"
1358+
"}");
1359+
ASSERT_EQUALS("", errout.str());
13511360
}
13521361

13531362
void stlBoundaries2() {

0 commit comments

Comments
 (0)