Menu ▾ ▴

functionStatic false positive

CJ Wood
2026-10-03
2026-10-05
  • CJ Wood

    CJ Wood - 2026-10-03

    I see a false positive running cppcheck 2.22.0 against the following code. Note the function itself cannot be static because it's an explicit member function (deducing this).

    #include <array>
    #include <cstddef>
    #include <ranges>
    #include <span>
    
    constexpr std::size_t GridSize{19};
    
    class board
    {
    public:
        auto rows(this auto&& self)
        {
            auto* cells = self.data.data();
            return std::views::iota(0zu, GridSize)
                | std::views::transform([cells](std::size_t row) {
                    return std::span{cells + row * GridSize, GridSize};
                });
        }
    
    private:
        std::array<int, GridSize * GridSize> data{};
    };
    
    int main()
    {
        board value;
        const auto rows = value.rows();
        const auto firstRow = *rows.begin();
        (void)firstRow;
    }
    

    cppcheck output:

    cppcheck_deducing_this_rows.cpp:11:10: style: The member function 'board::rows' can be static. [functionStatic]
        auto rows(this auto&& self)
             ^
    
     
  • CHR

    CHR - 2026-10-05

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

     

Log in to post a comment.