diff --git a/changelog/change_deprecation_for_cop_cop_cops.md b/changelog/change_deprecation_for_cop_cop_cops.md new file mode 100644 index 00000000000..1d947c35874 --- /dev/null +++ b/changelog/change_deprecation_for_cop_cop_cops.md @@ -0,0 +1 @@ +* [#13253](https://github.com/rubocop/rubocop/pull/13253): Emit a deprecation when custom cops inherit from `RuboCop::Cop::Cop`. ([@earlopain][]) diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb index 1f853603a27..dba0f636415 100644 --- a/lib/rubocop/cop/cop.rb +++ b/lib/rubocop/cop/cop.rb @@ -22,6 +22,14 @@ def call(corrector) end end + def self.inherited(_subclass) + super + warn Rainbow(<<~WARNING).yellow, uplevel: 1 + Inheriting from `RuboCop::Cop::Cop` is deprecated. Use `RuboCop::Cop::Base` instead. + For more information, see https://docs.rubocop.org/rubocop/v1_upgrade_notes.html. + WARNING + end + def self.support_autocorrect? method_defined?(:autocorrect) end diff --git a/spec/rubocop/cop/cop_spec.rb b/spec/rubocop/cop/cop_spec.rb index abd4b42167f..84b8c1522e7 100644 --- a/spec/rubocop/cop/cop_spec.rb +++ b/spec/rubocop/cop/cop_spec.rb @@ -233,9 +233,13 @@ context 'when cop supports autocorrection', :restore_registry do let(:cop_class) do - stub_cop_class('RuboCop::Cop::Test::StubCop', inherit: described_class) do - def autocorrect(node); end - end + cop_class = nil + expect do # rubocop:disable RSpec/ExpectInLet + cop_class = stub_cop_class('RuboCop::Cop::Test::StubCop', inherit: described_class) do + def autocorrect(node); end + end + end.to output(/Inheriting from `RuboCop::Cop::Cop` is deprecated/).to_stderr + cop_class end context 'when offense was corrected' do