Menu ▾ ▴

False constParameterPointer flag for QMap::insert

Long Huang
3 days ago
10 hours ago
  • Long Huang

    Long Huang - 3 days ago

    Minimal reproduction:

    #ifdef KNOWN_MAP_DECLARATION
    template<typename Key, typename Value>
    class QMap
    {
    public:
       void insert(const Key&, const Value&);
       Value value(const Key&) const;
    };
    #else
    #include <QMap>
    #endif
    
    struct Client
    {
       void Update();
    };
    
    class Registry
    {
    public:
       void Add(unsigned id, Client* client);
       void Update(unsigned id) const;
    
    private:
       QMap<unsigned, Client*> Clients;
    };
    
    void Registry::Add(unsigned id, Client* client)
    {
       Clients.insert(id, client);
    }
    
    void Registry::Update(unsigned id) const
    {
       Clients.value(id)->Update();
    }
    

    Save as repro.cpp, then run:

    cppcheck --enable=style --inconclusive --library=qt --std=c++14 repro.cpp
    

    Actual:

    style: Parameter 'client' can be declared as pointer to const [constParameterPointer]
    

    Expected:

    No warning. The suggestion is invalid: the map stores Client*, not const Client*. For QMap::insert(const Key&, const T&), substituting T = Client* makes the second parameter Client* const&. It does not permit a pointer to a const client. The stored clients are subsequently used for non-const calls.

     
  • CHR

    CHR - 10 hours ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15185

     

Log in to post a comment.