Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8627] Fix a false positive for Lint/DuplicateRequire #8631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#8627](https://github.com/rubocop-hq/rubocop/issues/8627): Fix a false positive for `Lint/DuplicateRequire` when same feature argument but different require method. ([@koic][])

## 0.90.0 (2020-09-01)

### New features
Expand Down
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -805,6 +805,10 @@ require 'foo'
# good
require 'foo'
require 'bar'

# good
require 'foo'
require_relative 'foo'
----

== Lint/DuplicateRescueException
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/duplicate_require.rb
Expand Up @@ -15,6 +15,10 @@ module Lint
# require 'foo'
# require 'bar'
#
# # good
# require 'foo'
# require_relative 'foo'
#
class DuplicateRequire < Base
MSG = 'Duplicate `%<method>s` detected.'
REQUIRE_METHODS = %i[require require_relative].freeze
Expand All @@ -31,7 +35,7 @@ def on_new_investigation

def on_send(node)
return unless REQUIRE_METHODS.include?(node.method_name) && require_call?(node)
return if @required[node.parent].add?(node.first_argument)
return if @required[node.parent].add?("#{node.method_name}#{node.first_argument}")

add_offense(node, message: format(MSG, method: node.method_name))
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/lint/duplicate_require_spec.rb
Expand Up @@ -73,6 +73,13 @@ def m
RUBY
end

it 'does not register an offense when same feature argument but different require method' do
expect_no_offenses(<<~RUBY)
require 'feature'
require_relative 'feature'
RUBY
end

it 'does not register an offense when calling user-defined `require` method' do
expect_no_offenses(<<~RUBY)
params.require(:user)
Expand Down