Menu

2 false positives in 1 unittest

2024-07-05
2024-07-05
  • Rob Deckers

    Rob Deckers - 2024-07-05

    Using CppCheck 2.14 and implementing a performance measuring unittest, I seem to be getting 2 different false positives.

    TEST(FilterTest, PerformanceTest)
    {
        const size_t window = 3;
        const size_t num_samples = 1000000;
    
        Filter<double, window> median;
        std::vector<double> samples(num_samples);
    
        double result;
        for (const auto& sample : samples) {
            result = median.update(sample);
        }
    
        (void)result;
    }
    

    Cppcheck result:
    component_projects\bab_shared\unittest\filters\FilterTest.cpp:44:16: style: Consider using std::accumulate algorithm instead of a raw loop. [useStlAlgorithm]
    result = median.update(sample);
    ^
    component_projects\bab_shared\unittest\filters\FilterTest.cpp:36:30: style: Variable 'window_size' is assigned a value that is never used. [unreadVariable]
    const size_t window_size = 3; // Odd number
    ^

    window_size IS used in the template of FILTER template. and the for loop is not accumulating, we are just running update and effectively ignoring the result value.

    When I bring the (void)result inside the loop, the notification of std::accumulate is gone.

     

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.