diff --git a/CHANGELOG.md b/CHANGELOG.md index cc0bfc53a5c..d89af08ea1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Bug fixes +* [#6254](https://github.com/rubocop-hq/rubocop/issues/6254): Fix `Layout/RescueEnsureAlignment` for non-local assignments. ([@marcotc][]) * [#6678](https://github.com/rubocop-hq/rubocop/issues/6678): Fix `Lint/DisjunctiveAssignmentInConstructor` when it finds an empty constructor. ([@rmm5t][]) * Do not attempt to auto-correct mass assignment or optional assignment in `Rails/RelativeDateConstant`. ([@rrosenblum][]) * Fix auto-correction of `Style/WordArray` and `Style/SymbolArray` when all elements are on separate lines and there is a trailing comment after the closing bracket. ([@rrosenblum][]) @@ -3769,3 +3770,4 @@ [@Ruffeng]: https://github.com/Ruffeng [@roooodcastro]: https://github.com/roooodcastro [@rmm5t]: https://github.com/rmm5t +[@marcotc]: https://github.com/marcotc diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index 1b981e3b931..7984c43eac0 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -30,7 +30,6 @@ class RescueEnsureAlignment < Cop ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + %i[block]).freeze ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze - ASSIGNMENT_TYPES = %i[lvasgn].freeze def on_resbody(node) check(node) unless modifier?(node) @@ -141,8 +140,7 @@ def ancestor_node(node) def assignment_node(node) assignment_node = node.ancestors.first return nil unless - assignment_node && - ASSIGNMENT_TYPES.include?(assignment_node.type) + assignment_node && assignment_node.assignment? assignment_node end diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 4c042370c41..e104c8b4788 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -375,7 +375,7 @@ def method2 RUBY end - it 'accepts aligned rescue in assigned do-end block' do + it 'accepts aligned rescue do-end block assigned to local variable' do expect_no_offenses(<<-RUBY.strip_indent) result = [1, 2, 3].map do |el| el.to_s @@ -385,6 +385,70 @@ def method2 RUBY end + it 'accepts aligned rescue in do-end block assigned to instance variable' do + expect_no_offenses(<<-RUBY.strip_indent) + @instance = [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block assigned to class variable' do + expect_no_offenses(<<-RUBY.strip_indent) + @@class = [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block assigned to global variable' do + expect_no_offenses(<<-RUBY.strip_indent) + $global = [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block assigned to class' do + expect_no_offenses(<<-RUBY.strip_indent) + CLASS = [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block on multi-assignment' do + expect_no_offenses(<<-RUBY.strip_indent) + a, b = [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block on operation assignment' do + expect_no_offenses(<<-RUBY.strip_indent) + a += [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block on and-assignment' do + expect_no_offenses(<<-RUBY.strip_indent) + a &&= [].map do |_| + rescue StandardError => _ + end + RUBY + end + + it 'accepts aligned rescue in do-end block on or-assignment' do + expect_no_offenses(<<-RUBY.strip_indent) + a ||= [].map do |_| + rescue StandardError => _ + end + RUBY + end + it 'accepts aligned rescue in assigned do-end block starting on newline' do expect_no_offenses(<<-RUBY.strip_indent) valid =