From 69700a544bcb15079f177ce5df4f808a61f1949f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 5 Sep 2020 11:20:23 +0900 Subject: [PATCH] Fix a false positive for `Layout/DefEndAlignment` 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 --- CHANGELOG.md | 1 + lib/rubocop/cop/layout/def_end_alignment.rb | 2 +- spec/rubocop/cop/layout/def_end_alignment_spec.rb | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebbfe72bf2d..46254b1fb36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/layout/def_end_alignment.rb b/lib/rubocop/cop/layout/def_end_alignment.rb index 873f68c319d..92632000bcf 100644 --- a/lib/rubocop/cop/layout/def_end_alignment.rb +++ b/lib/rubocop/cop/layout/def_end_alignment.rb @@ -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 diff --git a/spec/rubocop/cop/layout/def_end_alignment_spec.rb b/spec/rubocop/cop/layout/def_end_alignment_spec.rb index 1ef5e2cc562..89161cdcef2 100644 --- a/spec/rubocop/cop/layout/def_end_alignment_spec.rb +++ b/spec/rubocop/cop/layout/def_end_alignment_spec.rb @@ -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