Skip to content

Commit

Permalink
[Fix rubocop#9342] Fix an error for Lint/RedundantDirGlobSort
Browse files Browse the repository at this point in the history
Fixes rubocop#9342.

This PR fixes an error for `Lint/RedundantDirGlobSort`
when using `collection.sort`.
  • Loading branch information
koic committed Jan 7, 2021
1 parent 51d84ee commit 703325b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_redundant_dir_glob_sort.md
@@ -0,0 +1 @@
* [#9342](https://github.com/rubocop-hq/rubocop/issues/9342): Fix an error for `Lint/RedundantDirGlobSort` when using `collection.sort`. ([@koic][])
8 changes: 5 additions & 3 deletions lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
Expand Up @@ -34,11 +34,13 @@ class RedundantDirGlobSort < Base

def on_send(node)
return unless (receiver = node.receiver)
return unless receiver.receiver.const_type? && receiver.receiver.short_name == :Dir
return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
return unless GLOB_METHODS.include?(receiver.method_name)

add_offense(node.loc.selector) do |corrector|
corrector.remove(node.loc.selector)
selector = node.loc.selector

add_offense(selector) do |corrector|
corrector.remove(selector)
corrector.remove(node.loc.dot)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/redundant_dir_glob_sort_spec.rb
Expand Up @@ -54,6 +54,12 @@
end
RUBY
end

it 'does not register an offense when using `collection.sort`' do
expect_no_offenses(<<~RUBY)
collection.sort
RUBY
end
end

context 'when Ruby 2.7 or lower', :ruby27 do
Expand Down

0 comments on commit 703325b

Please sign in to comment.