Menu

What is the intention of the constArgument warning?

2019-02-13
2019-02-19
  • Steven Cook

    Steven Cook - 2019-02-13

    If I run cppcheck 1.87 on the following code it warns me that the argument to setw is always 10. What is the intention of this? For now I have disabled this check because it doesn't seem to add any value.

    #include <iostream>
    #include <iomanip>
    
    int main( int argc, char* argv[] )
    {
        const int columnWidths[] = { 10, 10 };
        std::cout << std::setw( columnWidths[0] ) << "Name"
                  << std::setw( columnWidths[1] ) << "Value" << std::endl;
        std::cout << std::setw( columnWidths[0] ) << "x"
                  << std::setw( columnWidths[1] ) << 42 << std::endl;
        return 0;
    }
    
    [cppcheck_constargument.cpp:7]: (style) Argument 'columnWidths[0]' to function setw is always 10
    [cppcheck_constargument.cpp:8]: (style) Argument 'columnWidths[1]' to function setw is always 10
    [cppcheck_constargument.cpp:9]: (style) Argument 'columnWidths[0]' to function setw is always 10
    [cppcheck_constargument.cpp:10]: (style) Argument 'columnWidths[1]' to function setw is always 10
    
     
  • versat

    versat - 2019-02-19

    IMHO your example shows a false positive. Maybe the check can be improved to not warn in such cases.
    Looking at the test cases for examples where Cppcheck should warn shows that there are cases where it makes sense to warn:

    void g(int);
    void f(int x) {
        g((x & 0x01) >> 7);
    }
    

    Here the argument was not meant to be constant. So it is a real error.
    The corresponding ticket with the idea is here:
    https://trac.cppcheck.net/ticket/8830

     
  • versat

    versat - 2019-02-19
     
  • Daniel Marjamäki

    I agree.. it is a false positive.

     

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.