diff --git a/changelog/fix_an_error_for_lint_require_relative_self_path.md b/changelog/fix_an_error_for_lint_require_relative_self_path.md new file mode 100644 index 00000000000..153e0badc36 --- /dev/null +++ b/changelog/fix_an_error_for_lint_require_relative_self_path.md @@ -0,0 +1 @@ +* [#10143](https://github.com/rubocop/rubocop/issues/10143): Fix an error for `Lint/RequireRelativeSelfPath` when using a variable as an argument of `require_relative`. ([@koic][]) diff --git a/lib/rubocop/cop/lint/require_relative_self_path.rb b/lib/rubocop/cop/lint/require_relative_self_path.rb index ad0a877cac8..585f0038f8b 100644 --- a/lib/rubocop/cop/lint/require_relative_self_path.rb +++ b/lib/rubocop/cop/lint/require_relative_self_path.rb @@ -27,6 +27,7 @@ class RequireRelativeSelfPath < Base def on_send(node) return unless (required_feature = node.first_argument) + return unless required_feature.respond_to?(:value) return unless same_file?(processed_source.file_path, required_feature.value) add_offense(node) do |corrector| diff --git a/spec/rubocop/cop/lint/require_relative_self_path_spec.rb b/spec/rubocop/cop/lint/require_relative_self_path_spec.rb index 5f080e10126..63628802533 100644 --- a/spec/rubocop/cop/lint/require_relative_self_path_spec.rb +++ b/spec/rubocop/cop/lint/require_relative_self_path_spec.rb @@ -42,4 +42,10 @@ require_relative 'foo.racc' RUBY end + + it 'does not register an offense when using a variable as an argument of `require_relative`' do + expect_no_offenses(<<~RUBY, 'foo.rb') + Dir['test/**/test_*.rb'].each { |f| require_relative f } + RUBY + end end