Menu

variableScope false positive ?

2018-05-02
2018-05-03
  • Sébastien Gallou

    Hello,

    I'm using CppCheck 1.83, and I get the variableScope message on this piece
    of code :

    auto nbOccurences = 0;
    auto isValid = false;
    std::for_each(
        m_recentlySeenDevices.begin(),
        m_recentlySeenDevices.end(),
        [&](std::pair<boost::posix_time::ptime, std::string> value)
        {
           if (value.second == deviceName)
           {
              ++nbOccurences;
              if (nbOccurences >= m_minTimesPerPeriod)
                 isValid = true;
           }
        });
    

    The error occurs on "nbOccurences" variable, which I don't see how to
    reduce scope without breaking the functionnal.

    Is this a false positive ?

    Thanks,

    S. Gallou

     

    Last edit: Sébastien Gallou 2018-05-02
  • versat

    versat - 2018-05-02

    Can you please edit the post and add code formatting syntax, for example:

    ~~~
    code goes here
    ~~~

    Otherwise some characters can get wrongly interpreted which results in a wrong source code.

     

    Last edit: versat 2018-05-02
  • Sébastien Gallou

    Yes, you're right.
    Done.

     
  • versat

    versat - 2018-05-03

    If i use exactly your source code i do not get a false positive with Cppcheck 1.83 or current development version.

    If the code is inside a function then i do get the variableScope message:

    void f()
    {
        auto nbOccurences = 0;
        auto isValid = false;
        std::for_each(
            m_recentlySeenDevices.begin(),
            m_recentlySeenDevices.end(),
            [&](std::pair<boost::posix_time::ptime, std::string> value)
            {
                if (value.second == deviceName)
                {
                    ++nbOccurences;
                    if (nbOccurences >= m_minTimesPerPeriod)
                        isValid = true;
                }
            });
    }
    
    $ ./cppcheck --enable=all fp_variableScope.cpp
    Checking fp_variableScope.cpp ...
    [fp_variableScope.cpp:3]: (style) The scope of the variable 'nbOccurences' can be reduced.
    [fp_variableScope.cpp:1]: (style) The function 'f' is never used.
    
     

    Last edit: versat 2018-05-03
  • versat

    versat - 2018-05-03

    I tried to reduce the code so it does not use boost and does not error out via g++ -fsyntax-only.
    For this code:

    #include <vector>
    #include <algorithm>
    
    bool f(std::vector<int> vec, int someValue)
    {
        auto nbOccurences = 0;
        auto isValid = false;
        std::for_each(
            vec.begin(),
            vec.end(),
            [&](int value)
            {
                if (value == someValue)
                {
                    ++nbOccurences;
                    if (nbOccurences >= 5)
                    {
                        isValid = true;
                    }
                }
            });
        return isValid;
    }
    

    Cppcheck reports the following:

    $ g++ -fsyntax-only fp_variableScope.cpp
    
    $ ./cppcheck --enable=all fp_variableScope.cpp
    Checking fp_variableScope.cpp ...
    [fp_variableScope.cpp:6]: (style) The scope of the variable 'nbOccurences' can be reduced.
    [fp_variableScope.cpp:4]: (style) The function 'f' is never used.
    (information) Cppcheck cannot find all the include files (use --check-config for details)
    

    This really seems to be a false positive.

     
  • versat

    versat - 2018-05-03

    I created the ticket 8541 for this issue.

     
  • Sébastien Gallou

    Thanks a lot, I will follow it.

     

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.