From 021b2368d968901a01da011b8bff5a165c556e10 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Fri, 20 Sep 2024 09:27:20 +0200 Subject: [PATCH] Emit a deprecation when custom cops inherit from `RuboCop::Cop::Cop` This has been long-marked as deprecated but not everyone has gotten the memo: https://github.com/search?q=%2F%3C+RuboCop%3A%3ACop%3A%3ACop%2F+lang%3Aruby+-is%3Afork&type=code Some methods _are_ deprecated but its perfectly possible to just not use any of them. --- changelog/change_deprecation_for_cop_cop_cops.md | 1 + lib/rubocop/cop/cop.rb | 8 ++++++++ spec/rubocop/cop/cop_spec.rb | 10 +++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 changelog/change_deprecation_for_cop_cop_cops.md 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 000000000000..1d947c358740 --- /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 1f853603a279..dba0f636415f 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 abd4b42167fc..84b8c1522e7a 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