Menu

cppcheck suppress

2020-09-08
2020-09-11
  • jeniffer1234

    jeniffer1234 - 2020-09-08

    Not sure if I am doing something wrong.
    Code example below.

    #include <boost/foreach.hpp>
    #include <vector>
    
    struct Test
    {
    int member;
    };
    
    void XX()
    {
       std::vector<Test> tests;
       tests.push_back(Test());
       int some_value = 10;
       BOOST_FOREACH(const Test& test, tests)
       {
          if (test.member == some_value)
          {
             return;
          }
       }
    }
    

    I am getting:
    style/useStlAlgorithm: Consider using std::anyof algorithm instead of a raw loop.
    This is OK and I accept this "problem" but I would like to leave this code as it is.
    So I am trying to use cppcheck-suppress but where?
    I tried different places but none of them suppress this warning.

    For example (different places for suppression)

    #include <boost/foreach.hpp>
    #include <vector>
    
    struct Test
    {
    int member;
    };
    
    void XX()
    {
       std::vector<Test> tests;
       tests.push_back(Test());
       int some_value = 10;
       // cppcheck-suppress useStlAlgorithm
       BOOST_FOREACH(const Test& test, tests)
       {
          // cppcheck-suppress useStlAlgorithm
          if (test.member == some_value)
          {
             // cppcheck-suppress useStlAlgorithm
             return;
          }
       }
    }
    

    None of them suppress this warning. Where should I put this cppcheck-suppress useStlAlgorithm?

     
  • Daniel Marjamäki

    It seems likely to me that you want to suppress such warnings in all files. Then using --suppress=useStlAlgorithm would be better.

    hmm.. I fail to inline suppress this warning also.

     

    Last edit: Daniel Marjamäki 2020-09-09
  • jeniffer1234

    jeniffer1234 - 2020-09-09

    Thank you for your quick response.
    In most cases I would like to have this warning enabled that's why I want to suppress only in specific places.

     
  • Daniel Marjamäki

    It will be fixed in cppcheck-2.2 .. you have two options either put the comment on the line before the warning or put the comment on the same line as the warning:

       BOOST_FOREACH(const Test& test, tests)
       // cppcheck-suppress useStlAlgorithm
       { // cppcheck-suppress useStlAlgorithm
          if (test.member == some_value)
          {
             return;
          }
       }
    
     

    Last edit: Daniel Marjamäki 2020-09-11

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.