Skip to content

Commit b391765

Browse files
Fix #14725 FP redundantIfRemove when conditional code has extra code (#8789)
1 parent 4184c39 commit b391765

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

lib/checkstl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,9 @@ void CheckStlImpl::redundantCondition()
18671867
const Token *var3 = var2->tokAt(7);
18681868
const Token *any2 = var3->tokAt(4);
18691869

1870+
if (any2->tokAt(3) != scope.bodyEnd)
1871+
continue;
1872+
18701873
// Check if all the "%name%" fields are the same and if all the "%any%" are the same..
18711874
if (var1->str() == var2->str() &&
18721875
var2->str() == var3->str() &&

test/teststl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4556,6 +4556,16 @@ class TestStl : public TestFixture {
45564556
" haystack.remove(needle);"
45574557
"}");
45584558
ASSERT_EQUALS("[test.cpp:3:9]: (style) Redundant checking of STL container element existence before removing it. [redundantIfRemove]\n", errout_str());
4559+
4560+
check("void g(const std::string&);\n" // #14725
4561+
"std::set<std::string> g_s;\n"
4562+
"void f(const std::string& k) {\n"
4563+
" if (g_s.find(k) != g_s.end()) {\n"
4564+
" g_s.erase(k);\n"
4565+
" g(k);\n"
4566+
" }\n"
4567+
"}");
4568+
ASSERT_EQUALS("", errout_str());
45594569
}
45604570

45614571
void missingInnerComparison1() {

0 commit comments

Comments
 (0)