Opened 4 months ago

Closed 9 days ago

#14725 closed defect (fixed)

False positive: redundantIfRemove when conditional code has extra code

Reported by: Daniel Marjamäki Owned by: noone
Priority: Normal Milestone: 2.22
Component: False positive Version: 1.65
Keywords: redundantIfRemove simple Cc:

Description (last modified by kidkat)

Example code:

#include <set>
#include <string>

void unsubscribe(const std::string& key);

std::set<std::string> g_params;

void del(const std::string& key) {
    if (g_params.find(key) != g_params.end()) {
        g_params.erase(key);
        unsubscribe(key); // only call if key was present
    }
}

Command:

cppcheck --enable=style fp.cpp

False positive:

fp.cpp:9:9: style: Redundant checking of STL container element existence before removing it. [redundantIfRemove]
    if (g_params.find(key) != g_params.end()) {
        ^

Without a check, the unsubscribe function would always be called.

Change History (3)

comment:1 by chrchr, 4 months ago

Keywords: simple added

Also, the only test case we have is based on a non-existent string::remove() method.

E.g. QSet has remove(), but currently there is no validation of the container.

Last edited 12 days ago by chrchr (previous) (diff)

comment:2 by kidkat, 4 months ago

Description: modified (diff)
Version: 1.65

comment:3 by chrchr, 9 days ago

Milestone: 2.22
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.