Menu

Unused private function false positive but it is used in a "compare" functor

Roy
2021-04-28
2021-04-28
  • Roy

    Roy - 2021-04-28

    Code :

    struct key_compare;
    struct key {
      bool top;
      int serial_num;
    
      friend struct key_compare;
    
    private:
      size_t get_hash() const { return (serial_num >> 1) + (top << 10); }
    };
    
    struct key_compare {
      bool operator()(const key &lhs, const key &rhs) const {
        return lhs.get_hash() < rhs.get_hash();
      }
    };
    
    struct items {
      int a;
      int b;
      int c;
      std::string g;
    }
    
    std::map<key, items, key_compare>
        map_;
    
    int main() {
      map_.insert_or_assign({true, 1}, items());
      map_.insert_or_assign({true, 2}, items());
    }
    

    Cpp check output:

    foo.cpp:9:10: style: Unused private function: 'key::get_hash' [unusedPrivateFunction]
      size_t get_hash() const { return (serial_num >> 1) + (top << 10); }
    

    cppcheck version : 2.4.1

     
  • Daniel Marjamäki

    Thanks! I can reproduce. I have created ticket https://trac.cppcheck.net/ticket/10265

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.