Skip to content

Commit

Permalink
Merge pull request #8631 from koic/fix_false_positive_for_lint_duplic…
Browse files Browse the repository at this point in the history
…ate_require

[Fix #8627] Fix a false positive for `Lint/DuplicateRequire`
  • Loading branch information
koic committed Sep 2, 2020
2 parents ef3a603 + 2d7d47b commit 199294d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
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

0 comments on commit 199294d

Please sign in to comment.