Menu

False positive (Using object to temporary) when adding additional vector items

2020-09-07
2020-09-07
  • Fredrik Bondesson

    There seems to be another issue with false "Using obect to temporary" (using cppcheck 2.1 gui

    When only pushing one item to the vector, everything is fine, however adding additional items (uncoment comments in code below) , a "Using obect to temporary" error is issued, which I belive is not correct.

    #include <iostream>
    #include <vector>
    
    std::pair<std::string, std::string>
    str_pair(std::string const & a, std::string const & b)
    {
        return std::make_pair(a, b);
    }
    
    std::vector<std::pair<std::string, std::string> > create_parameters()
    {
        std::vector<std::pair<std::string, std::string> > par;
        par.push_back(str_pair("param1", "prop_a"));
        //par.push_back(str_pair("param2", "prop_b"));
        //par.push_back(str_pair("param3", "prop_c"));
        return par;
    }
    
    int main()
    {
        auto params = create_parameters();
        for (auto param: params)
        {
            std::cout<<param.first<<"\n";
        }
    }
    
     

    Last edit: Fredrik Bondesson 2020-09-07
  • Daniel Marjamäki

    hmm I fail to reproduce this one even with cppcheck-2.1. I get no warning. not sure what I could do wrongly. Do you see the warning with such command line: cppcheck --enable=style --inconclusive test.cpp ? or is something additional needed?

     
  • Fredrik Bondesson

    I used cppcheck ui, and analyze file.

    I can try it out again but meanwhile, just to be sure, you did uncomment the commented lines?

    Regards Fredrik

     
  • Daniel Marjamäki

    thanks! now I can reproduce.

     
  • Daniel Marjamäki

     

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.