Skip to content

Commit

Permalink
Fix a false positive for Layout/DefEndAlignment
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Layout/DefEndAlignment`
when using refinements and `private def`.
It prevents the following error when auto-correcting.

```console
% cat example.rb
using Module.new {
  refine Hash do
        class << Hash
      private def _ruby2_keywords_hash(*args)
      end
    end
  end
}

% bundle exec rubocop -a --only Layout/DefEndAlignment example.rb
(snip)

Inspecting 1 file
W

Offenses:

example.rb:5:7: W: [Corrected] Layout/DefEndAlignment: end at 5, 6 is
not aligned with using Module.new {
  refine Hash do
        class << Hash
      private def at 1, 0.
      end
      ^^^

0 files inspected, 1 offense detected, 1 offense corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/rails/example.rb.
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:289:in
`check_for_infinite_loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:272:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:242:in
`do_inspection_loop'
```

I found it with the following code.
https://github.com/rails/rails/blob/v6.0.3.2/activejob/lib/active_job/arguments.rb#L73-L96
  • Loading branch information
koic authored and bbatsov committed Sep 5, 2020
1 parent 0ea599b commit 69700a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#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][])
* [#8572](https://github.com/rubocop-hq/rubocop/issues/8572): Fix a false positive for `Style/RedundantParentheses` when parentheses are used like method argument parentheses. ([@koic][])
* [#8653](https://github.com/rubocop-hq/rubocop/pull/8653): Fix a false positive for `Layout/DefEndAlignment` when using refinements and `private def`. ([@koic][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/def_end_alignment.rb
Expand Up @@ -46,7 +46,7 @@ def on_def(node)
alias on_defs on_def

def on_send(node)
return unless node.def_modifier?
return if !node.def_modifier? || node.method?(:using)

method_def = node.each_descendant(:def, :defs).first
expr = node.source_range
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/layout/def_end_alignment_spec.rb
Expand Up @@ -66,6 +66,21 @@ def Test.test
RUBY
end
end

context 'when using refinements and `private def`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
using Module.new {
refine Hash do
class << Hash
private def _ruby2_keywords_hash(*args)
end
end
end
}
RUBY
end
end
end

context 'when EnforcedStyleAlignWith is def' do
Expand Down

0 comments on commit 69700a5

Please sign in to comment.