Menu

False positive: functionStatic with included template member function

2020-11-09
2020-11-11
  • Andreas Grob

    Andreas Grob - 2020-11-09

    a.hpp:

    struct A {
        int a = 1;
        void f() { g(1); }
        template <typename T> void g(T x) { a = 2; }
    };
    

    test.cpp:

    #include "a.hpp"
    
    int main() {
        A a;
        a.f();
    }
    

    With 7e8f405:

    $ cppcheck --enable=all --inconclusive test.cpp
    Checking test.cpp ...
    a.hpp:3:10: performance:inconclusive: Technically the member function 'A::f' can be static (but you may consider moving to unnamed namespace). [functionStatic]
        void f() { g(1); }
             ^
    

    I am not sure how to write a test for this one, since the FP only occurs if both files are separate. If you merge both files, the FP disappears.
    Could you give me some pointers on how to write a test for this with two files?

     
  • Daniel Marjamäki

    look at the debug output. the template simplification is not made properly neither when the code is in the same file or not.

     
  • Daniel Marjamäki

    I created ticket: https://trac.cppcheck.net/ticket/9975

    I believe this should be fixed in the TemplateSimplifier. That is pretty complex code and I would say it's difficult to understand it. But feel free to take a look at it.

     
  • Robert Reif

    Robert Reif - 2020-11-10
     
  • Andreas Grob

    Andreas Grob - 2020-11-10

    That fix doesn't work for me, the error persists as described in the ticket.

     
  • Andreas Grob

    Andreas Grob - 2020-11-10

    The test case probably does not reflect the issue of having the struct in a header. I have no idea if you can even write such a test.

     
  • Daniel Marjamäki

    No worry.. Robert fixed the issue I reported.. we can fix more issues iteratively..

     
  • Daniel Marjamäki

     
  • Robert Reif

    Robert Reif - 2020-11-11

    Templates are automatically erased from header files and there is no way to change that behavior from the command line. See https://trac.cppcheck.net/ticket/9977

     
  • Daniel Marjamäki

    I have fixed 9977 and now I can't reproduce the FP functionStatic warning.

     

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.