Menu

Compilation of cppcheck false positives (functionStatic, virtualCallInConstructor, knownConditionTrueFalse, duplicateExpression)

Long Huang
2026-08-03
2026-08-04
  • Long Huang

    Long Huang - 2026-08-03
    Post awaiting moderation.
  • Long Huang

    Long Huang - 2026-08-03
    Post awaiting moderation.
  • Long Huang

    Long Huang - 2026-08-03
    Post awaiting moderation.
  • Long Huang

    Long Huang - 2026-08-04

    9) danglingLifetime doesn't account for ownership being transferred to a longer-lived owner via std::move immediately after capturing a raw pointer

    Minimal reproduction:

    #include <memory>
    
    class Bar
    {
    public:
       virtual ~Bar() = default;
    };
    
    class Consumer
    {
    public:
       explicit Consumer(std::unique_ptr<Bar> bar)
          : Owned(std::move(bar))
       {
       }
    
    private:
       std::unique_ptr<Bar> Owned;
    };
    
    class Foo
    {
    public:
       void CreateConsumer()
       {
          std::unique_ptr<Bar> tmp = std::make_unique<Bar>();
          RawPtr = tmp.get();
          ConsumerInstance.reset(new Consumer(std::move(tmp)));
       }
    
    private:
       Bar* RawPtr = nullptr;
       std::unique_ptr<Consumer> ConsumerInstance;
    };
    

    RawPtr = tmp.get() captures the address of the heap object owned by the local tmp. On the very next line, std::move(tmp) transfers that ownership into Consumer::Owned, which is stored inside ConsumerInstance, a member with a lifetime far longer than the local tmp variable.


    10) functionStatic doesn't detect that a member function requires an instance when its only "member use" is an unqualified call to a non-static sibling overload

    Minimal reproduction:

    class Widget
    {
    public:
       void Notify(int id, const int& value)
       {
          Sink = value; // touches a member, so this overload is correctly non-static
       }
    
       void Notify(int id, int a = 0, int b = 1)
       {
          int value = a + b;
          Notify(id, value); // unqualified call, resolves to the non-static overload above
       }
    
    private:
       int Sink = 0;
    };
    

    cppcheck reports that the second Notify overload "can be static". The unqualified call Notify(id, value) is equivalent to this->Notify(id, value) and resolves via overload resolution to the non-static overload that mutates Sink. Since it needs this it can't be static.

    Expected: No functionStatic warning, since the function needs an object instance from the call to the non-static overload.

     

    Last edit: Long Huang 2026-08-04
  • Aaron Danen

    Aaron Danen - 2026-08-04

    Hah I was just trying to make a minimal example for the dangling lifetime issue. Here is what I have right now:

    class A {};
    
    std::unique_ptr<A> saved;
    
    A *f(int x) {
      std::unique_ptr<A> tmp = std::make_unique<A>();
      A *ptr = tmp.get();
      saved = std::move(tmp);
      return ptr;
    }
    

    expected result: no errors
    actual result: error: Returning pointer to local variable 'tmp' that will be invalid when returning. [returnDanglingLifetime]

     
  • CHR

    CHR - 2026-08-04

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

     
    • Long Huang

      Long Huang - 2026-08-04

      Thank you, there're a few more in the initial and 2nd posts that are still awaiting sourceforge moderator approval in this thread.

       
  • CHR

    CHR - 2026-08-04

    10) functionStatic doesn't detect that a member function requires an instance when its only "member use" is an unqualified call to a non-static sibling overload

    The provided example does not compile.

     

Log in to post a comment.