Menu

False positive - const variable

biwipo4118
2020-09-29
2020-09-30
  • biwipo4118

    biwipo4118 - 2020-09-29

    Correct me If I am wrong but this is probably false positive.

    #include <map>
    #include <gmock/gmock.h>
    #include <gtest/gtest.h>
    
    typedef std::map<char, const char*> map_test;
    
    struct Object
    {
       Object() : map() {}
       map_test map;
    };
    
    map_test func() { return map_test(); }
    
    TEST(test, test)
    {
       Object test_object;
       map_test& standard = test_object.map;
       EXPECT_STREQ("some_value", standard['c']);
    }
    

    Cppcheck:
    style/constVariable: Variable 'standard' can be declared with const

    Adding const to "standard" variable does not compile.

    Using --library=googletest in cppcheck arguments.

     

    Last edit: biwipo4118 2020-09-29
  • Daniel Marjamäki

    Thanks! I think this has been fixed. I can reproduce with cppcheck-2.0 but not with cppcheck HEAD.

    danielm@edge:~/cppcheck$ ./cppcheck --library=googletest fp1.cpp --enable=style
    Checking fp1.cpp ...
    danielm@edge:~/cppcheck$ ../.cppcheck/cppcheck-2.0 --library=googletest fp1.cpp --enable=style
    Checking fp1.cpp ...
    fp1.cpp:15:14: style: Variable 'standard' can be declared with const [constVariable]
       map_test& standard = test_object.map;
                 ^
    
     
  • pokopa2803

    pokopa2803 - 2020-09-30

    Thanks for quick response!
    I can reproduce this with cppcheck-2.1

     

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.