Menu

[Possible false positive]

2024-07-22
2024-07-23
  • Tobias Markus

    Tobias Markus - 2024-07-22

    I found out that the code here:
    https://coliru.stacked-crooked.com/view?id=4d0cc8073c899e6d

    #include <bitset>
    #include <iostream>
    
       // Not enum class since it must be used like a bitfield
      enum State
      {
        STATE_MAXIMIZED = 0,
        STATE_FULLSCREEN,
        STATE_RESIZING,
        STATE_ACTIVATED,
        STATE_COUNT
      };
      using StateBitset = std::bitset<STATE_COUNT>;
    
    int main()
    {
        StateBitset bitset;
        std::cout << "Bitset size: " << bitset.size() << '\n';
        std::cout << "size_t size: " << sizeof(size_t) << '\n';
    }
    
    void function(StateBitset bitset)
    {
        std::cout << bitset.size() << '\n';
    }
    

    yields a potential false positive performance warning when running

    % cppcheck --enable=performance ~/cppcheck_false_positive.cpp --check-level=exhaustive --force
    Checking /Users/tobiasmarkus/cppcheck_false_positive.cpp ...
    /Users/tobiasmarkus/cppcheck_false_positive.cpp:22:27: performance: Function parameter 'bitset' should be passed by const reference. [passedByValue]
    void function(StateBitset bitset)
                              ^
    

    The program's output is this:

    g++ -std=c++23  -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors main.cpp -lm  -latomic  && ./a.out
    
    Bitset size: 4
    
    size_t size: 8
    

    which means that passing the bitset by value is actually equally expensive as passing it by reference.

     
  • CHR

    CHR - 2024-07-23

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

     

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.