Menu

False positive "uninitMemberVarPrivate" or "noConstructor" with nested namespaces and using

2024-11-27
2024-11-27
  • Dan Workman

    Dan Workman - 2024-11-27

    Found in 2.15 and 2.16

    #include <iostream>
    
    namespace Foo
    {
      namespace Bar
      {
        class Baz
        {
          int i;
          int j{1};
        public:
          Baz() : i{0} {};
    
          int get_i() const  { return i; };
          int get_j() const  { return j; };
        };
      }
    
      using Baz = Bar::Baz;
    }
    
    int main()
    {
      Foo::Baz baz;
      std::cout << baz.get_i() << baz.get_j();
      return 0;
    }
    

    Output:

    C:\repos\cppcheck_false_positives>cppcheck cppcheck_false_positives.cpp --enable=all --suppress=missingIncludeSystem --template=gcc --library=windows.cfg --platform=win64 --std=c++20
    Checking cppcheck_false_positives.cpp ...
    cppcheck_false_positives.cpp:14:7: warning: Member variable 'Baz::i' is not initialized. [uninitMemberVarPrivate]
          int i;
          ^
    

    Or alternatively:

    namespace Foo
    {
      namespace Bar
      {
        class Baz
        {
          int i;
        public:
          Baz() : i{0} {};
          int get() const  { return i; };
        };
      }
    
      using Baz = Bar::Baz;
    }
    
    
    int main()
    {
      Foo::Baz baz;
      std::cout << baz.get();
      return 0;
    }
    

    Output:

    C:\cppcheck_false_positives>cppcheck cppcheck_false_positives.cpp --enable=all --suppress=missingIncludeSystem --template=gcc --library=windows.cfg --platform=win64 --std=c++20
    Checking cppcheck_false_positives.cpp ...
    cppcheck_false_positives.cpp:12:1: warning: The class 'Baz' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]
    class Baz
    ^
    
     
  • CHR

    CHR - 2024-11-27

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13354

     
    ❤️
    1

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.