Menu

[constParameterReference] false positive when using std::begin and std::end

2025-05-20
2025-05-21
  • Leander Schulten

    I have reduced the source file to the following:

            void test()
            {
                for (auto& maschSchichtData : ret)
                {
                    std::sort(std::begin(maschSchichtData), std::end(maschSchichtData));
                    //std::sort(maschSchichtData.begin(), maschSchichtData.end());
                }
            }
    

    And if I run cppcheck -i random_file.h --enable=all --project=all/all-vs2015.sln "--project-configuration=Debug|Win32" "--file-filter=*einrichtaufwand.cpp" I get

    ls@ws-dev-ls:~/git_projects/src$ cppcheck -i random_file.h --enable=all --project=all/all-vs2015.sln "--project-configuration=Debug|Win32" "--file-filter=*einrichtaufwand.cpp"
    cppcheck: filename exclusion does not apply to header (.h and .hpp) files.
    cppcheck: Please use --suppress for ignoring results from the header files.
    Checking lib/bdelib/haldia/einrichtaufwand.cpp Debug|Win32...
    Checking lib/bdelib/haldia/einrichtaufwand.cpp: _WIN32=1;__SSE2__=1;HAVE_SNPRINTF=1;BOOST_DATE_TIME_NO_LIB=1;_CRT_SECURE_NO_WARNINGS=1;__SSE2__=1;__SSE2__=1;__SSE2__=1;HAVE_SNPRINTF=1;BOOST_DATE_TIME_NO_LIB=1;_CRT_SECURE_NO_WARNINGS=1;__SSE2__=1;__SSE2__=1;WIN32=1;_DEBUG=1;_WINDOWS=1;__SSE2__=1;_MSC_VER=1900;__AFXWIN_H__=1...
    lib/bdelib/haldia/einrichtaufwand.cpp:3:24: style: Variable 'maschSchichtData' can be declared as reference to const [constVariableReference]
                for (auto& maschSchichtData : ret)
                           ^
    lib/bdelib/haldia/einrichtaufwand.cpp:1:0: style: The function 'test' is never used. [unusedFunction]
            void test()
    ^
    nofile:0:0: information: Active checkers: 122/856 (use --checkers-report=<filename> to see details) [checkersReport]
    

    style: Variable 'maschSchichtData' can be declared as reference to const is wrong because you can not pass const pointers to std::sort.

    If I use std::sort(maschSchichtData.begin(), maschSchichtData.end()); instead I don't get this warning anymore.

    Strange things: If I remove -i random_file.h from the command line I don't get this error.
    If I use cppcheck -i random_file.h --enable=all lib/bdelib/haldia/einrichtaufwand.cpp I also don't get this error. I don't know why.

     
  • CHR

    CHR - 2025-05-21
     

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.