Menu

uselessOverride false positive that can cause behavioral issues

2024-05-03
2024-05-04
  • Steve Albright

    Steve Albright - 2024-05-03

    I fixed the warning and started to have test failures because the wrong function was being called in the base class vs. derived.

    I had a hard time getting a code example and left test support in.

    class UselessExample //: public Test
    {
    public:
       UselessExample() = default;
       ~UselessExample() = default;
    
    //   void SetUp() override
    //   {
    //      CreateObject();
    //   }
    
       virtual void CreateObject()
       {
          SetupTheAccessor();
       }
    
       virtual void SetupTheAccessor()
       {
          SetValues();
       }
    
       void SetValues() // not virtual
       {
       }
    
    };
    
    class UselessOverrideExample: public UselessExample
    {
    public:
       UselessOverrideExample() = default;
       ~UselessOverrideExample() = default;
    
       // style: uselessOverride - The function 'SetupTheAccessor' overrides a function in a base class but is identical to the overridden function
       // fixing the useless override warning causes wrong SetValues()
       void SetupTheAccessor() override
       {
          SetValues();
       }
    
       virtual void SetValues()// taking virtual off gives duplInheritedMember warning
       {
          // does something different than base
       }
    
    };
    
     
  • CHR

    CHR - 2024-05-04

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

     

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.