llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-tidy] bugprone-crtp-constructor-accessibility for deleted constructor

Open DimitrijeDobrota opened this issue 1 year ago • 2 comments

In this basic example of CRTP pattern:

template<typename D>
class base
{
  friend D;

  base() = default;
  base(const base&) = default;

  base(base&&) = delete;
};

class derived : public base<derived>
{
};

I get the following warning, which is expected and reasonable:

project_root/test.cpp:9:3: warning: deleted member function should be public [hicpp-use-equals-delete,modernize-use-equals-delete]
    9 |   base(base&&) = delete;
      |   ^

while on the other hand, if I were to move the constructor to the public area:

template<typename D>
class base
{
  friend D;

  base() = default;
  base(const base&) = default;

public:
  base(base&&) = delete;
};

class derived : public base<derived>
{
};

the following warning is not needed since the constructor has been deleted:

project_root/testcpp:10:3: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private [bugprone-crtp-constructor-accessibility]
   10 |   base(base&&) = delete;

DimitrijeDobrota avatar Apr 18 '25 22:04 DimitrijeDobrota

@llvm/issue-subscribers-clang-tidy

Author: Dimitrije Dobrota (DimitrijeDobrota)

In this basic example of CRTP pattern:
template&lt;typename D&gt;
class base
{
  friend D;

  base() = default;
  base(const base&amp;) = default;

  base(base&amp;&amp;) = delete;
};

class derived : public base&lt;derived&gt;
{
};

I get the following warning, which is expected and reasonable:

project_root/test.cpp:9:3: warning: deleted member function should be public [hicpp-use-equals-delete,modernize-use-equals-delete]
    9 |   base(base&amp;&amp;) = delete;
      |   ^

while on the other hand, if I were to move the constructor to the public area:

template&lt;typename D&gt;
class base
{
  friend D;

  base() = default;
  base(const base&amp;) = default;

public:
  base(base&amp;&amp;) = delete;
};

class derived : public base&lt;derived&gt;
{
};

the following warning is not needed since the constructor has been deleted:

project_root/testcpp:10:3: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private [bugprone-crtp-constructor-accessibility]
   10 |   base(base&amp;&amp;) = delete;

llvmbot avatar Apr 18 '25 22:04 llvmbot

Duplicate of https://github.com/llvm/llvm-project/issues/131737, PR in review state

vbvictor avatar Apr 19 '25 18:04 vbvictor