Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused private function false positive?

Tags:

c++

cppcheck

With Cppcheck I get:

Unused private function: 'Derived::Func'.

Is this a false positive?

#include <cstdint>

class Interface {
public:
  Interface() = default;
  virtual ~Interface() = default;
  Interface(const Interface &other) = delete;
  Interface(Interface &&other) = delete;
  auto operator=(const Interface &other) -> Interface & = delete;
  auto operator=(Interface &&other) -> Interface & = delete;

  virtual auto IFunc() -> void = 0;
};

class Derived final : public Interface {
public:
  auto IFunc() -> void override;
private:
  auto Func() -> void;

  std::uint16_t mMember{};
};

auto Derived::IFunc() -> void {
  Func();
  return;
}

auto Derived::Func() -> void {
  mMember = 42;
  return;
}

Derived::Func() should not be marked as unused because it is clearly used in Derived::IFunc().

like image 299
mbox Avatar asked Nov 15 '25 07:11

mbox


1 Answers

Is this a false positive?

Yes, it is, clearly. As you have stated, Derived::Func is used in Derived::IFunc.

Strangely, the warning disappears if you change the declaration (but not necessarily also the definition) of Derived::IFunc from using auto and the trailing return type specifier (-> void) to the 'old-fashioned', void IFunc() override;. At least, that fixes it in the online demo.

The warning also goes away if you just remove the definitions (out-of-class bodies) of IFunc and Func.

like image 161
Adrian Mole Avatar answered Nov 17 '25 21:11

Adrian Mole