diff --git a/changelog/fix_an_infinite_loop_error_for_layout_hash_alignment.md b/changelog/fix_an_infinite_loop_error_for_layout_hash_alignment.md new file mode 100644 index 00000000000..539ab1c4c28 --- /dev/null +++ b/changelog/fix_an_infinite_loop_error_for_layout_hash_alignment.md @@ -0,0 +1 @@ +* [#9954](https://github.com/rubocop/rubocop/issues/9954): Fix infinite loop error for `Layout/HashAlignment` when `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`. ([@koic][]) diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index ff6c6b0daa9..d5118517173 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -218,9 +218,13 @@ def on_hash(node) private def autocorrect_incompatible_with_other_cops?(node) - enforce_first_argument_with_fixed_indentation? && - node.pairs.any? && - node.parent&.call_type? && node.parent.loc.selector&.line == node.pairs.first.loc.line + return false unless enforce_first_argument_with_fixed_indentation? && + node.pairs.any? && + node.parent&.call_type? + + parent_loc = node.parent.loc + selector = parent_loc.selector || parent_loc.expression + selector.line == node.pairs.first.loc.line end def reset! diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 0ffc39c3100..862520b2d56 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -1875,6 +1875,9 @@ def do_even_more_stuff foo .do_something(foo: bar, baz: qux) + + do_something.(foo: bar, baz: qux, + quux: corge) RUBY create_file('.rubocop.yml', <<~YAML) @@ -1898,6 +1901,9 @@ def do_even_more_stuff foo .do_something(foo: bar, baz: qux) + + do_something.(foo: bar, baz: qux, + quux: corge) RUBY end