Menu

Nested type names clush in cppcheck analysis, causing misdetections and false possitives

Tal
2024-05-22
2024-06-03
  • Tal

    Tal - 2024-05-22

    I have been able to reproduce an error happened in our company with this checker.
    Using the online demo (https://cppcheck.sourceforge.io/demo/), for this code that declare the two nested structs A::Foo and B::Foo, cppcheck seems to be confused about the definition of Foo:

    #include <vector>
    #include <iostream>
    
    Class A {
    public:
      struct Foo { int a; int z; };
    
      void foo_adder();
    
      void print_all() {
        for (const auto& foo : m_foos) {
           std::cout << foo.a << foo.z << std::endl;
        }
      }
    
    private:
      std::vector<Foo> m_foos;
    };
    
    
    Class B {
    public:
      struct Foo { int b; int z; };
    
      void foo_adder();
    
      void print_all() {
        for (const auto& foo : m_foos) {
           std::cout << foo.b << foo.z << std::endl;
        }
      }
    
    private:
      std::vector<Foo> m_foos;
    };
    
    
    void A::foo_adder() {
      Foo foo;
      foo.a = 1;
      foo.z = 3;
      m_foos.push_back(foo);
    }
    
    void B::foo_adder() {
      B::Foo foo;
      foo.b = 2;
      foo.z = 3;
      m_foos.push_back(foo);
    }
    

    Yields:

    Cppcheck 2.10
    
    [test.cpp:23]: (style) struct member 'Foo::b' is never used.
    [test.cpp:23]: (style) struct member 'Foo::z' is never used.
    
    Done!
    
     
  • CHR

    CHR - 2024-05-22

    I can't reproduce this with 2.15dev.

     
    • Tal

      Tal - 2024-05-22

      Weird... I was testing this privately in V2.14, and the online demo with this toy example is V2.10.

      I will try to check this in a more updated version.

       
    • Tal

      Tal - 2024-06-03

      I also cannot reproduce it now

       

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.