Skip to content

Warnings are printed about methods being redefined when running specs that require 'rubocop/rspec/support' and which set RSpec 'config.warnings = true' #1465

@davidrunger

Description

@davidrunger

Warnings are printed about four methods being redefined, when running specs that require 'rubocop/rspec/support' and which configure RSpec with config.warnings = true in a project that has rubocop-rails in the Gemfile.

Below is a reproduction script. The script currently uses the latest released version of rubocop-rails (2.30.3), but it also has comments so that one can instead toggle in the first version in which this issue appeared (2.30.0), the most recent version that did not have this issue (2.29.1), and a branch on my fork that fixes the issue.

# File: redefined_methods_warnings_demo.rb
# Run with: `ruby redefined_methods_warnings_demo.rb`

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'

  # These are the latest released version of `rspec` and `rubocop`, at the time of writing (2025-03-12).
  gem 'rspec', '3.13.0'
  gem 'rubocop', '1.73.2', require: false

  # The warnings are printed when using the latest released version of rubocop-rails:
  gem 'rubocop-rails', '2.30.3', require: false
  # This is the first version in which the warnings appeared:
  # gem 'rubocop-rails', '2.30.0', require: false
  # The warnings are not printed when using version 2.29.1:
  # gem 'rubocop-rails', '2.29.1', require: false
  # The warnings are not printed using this branch of my davidrunger/rspec-rails fork:
  # gem 'rubocop-rails', require: false, github: 'davidrunger/rubocop-rails', branch: 'avoid-warnings-about-redefined-methods'
end

require 'rspec/autorun'
require 'rubocop'
require 'rubocop/rspec/support'

RSpec.configure do |config|
  # This is necessary for the warnings to be printed:
  config.warnings = true
end

puts("RuboCop version: #{RuboCop::Version.verbose}")

RSpec.describe('1 + 1') do
  specify { expect(1 + 1).to eq(2) }
end

Expected behavior

No warnings should be printed. The output of the above script should look something like this:

❯ ruby redefined_methods_warnings_demo.rb
RuboCop version: 1.73.2 (using Parser 3.3.7.1, rubocop-ast 1.38.1, analyzing as Ruby 2.7, running on ruby 3.4.2) [x86_64-linux]

1 + 1
  is expected to eq 2

Finished in 0.26631 seconds (files took 1.37 seconds to load)
1 example, 0 failures

Actual behavior

Warnings are printed about methods being redefined. The script above outputs this:

❯ ruby redefined_methods_warnings_demo.rb
RuboCop version: 1.73.2 (using Parser 3.3.7.1, rubocop-ast 1.38.1, analyzing as Ruby 2.7, running on ruby 3.4.2) [x86_64-linux]

1 + 1
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/activesupport-8.0.1/lib/active_support/core_ext/object/blank.rb:153: warning: method redefined; discarding old blank?
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-1.73.2/lib/rubocop/core_ext/string.rb:15: warning: previous definition of blank? was here
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.30.3/lib/rubocop/cop/mixin/enforce_superclass.rb:7: warning: method redefined; discarding old included
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-1.73.2/lib/rubocop/cop/mixin/enforce_superclass.rb:16: warning: previous definition of included was here
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.30.3/lib/rubocop/cop/mixin/enforce_superclass.rb:19: warning: method redefined; discarding old on_class
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-1.73.2/lib/rubocop/cop/mixin/enforce_superclass.rb:35: warning: previous definition of on_class was here
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-rails-2.30.3/lib/rubocop/cop/mixin/enforce_superclass.rb:25: warning: method redefined; discarding old on_send
/home/david/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/rubocop-1.73.2/lib/rubocop/cop/mixin/enforce_superclass.rb:39: warning: previous definition of on_send was here
  is expected to eq 2

Finished in 0.33684 seconds (files took 1.45 seconds to load)
1 example, 0 failures

Steps to reproduce the problem

Run the reproduction script above.

RuboCop version

  • rubocop version: 1.73.2 (using Parser 3.3.7.1, rubocop-ast 1.38.1, analyzing as Ruby 2.7, running on ruby 3.4.2) [x86_64-linux]
  • rubocop-rails version: 2.30.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions