diff --git a/CHANGELOG.md b/CHANGELOG.md index 75d6720fae9..23a1fc9dcad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#8720](https://github.com/rubocop-hq/rubocop/issues/8720): Fix an error for `Lint/IdentityComparison` when calling `object_id` method without receiver in LHS or RHS. ([@koic][]) +* [#8710](https://github.com/rubocop-hq/rubocop/issues/8710): Fix a false positive for `Layout/RescueEnsureAlignment` when `Layout/BeginEndAlignment` cop is not enabled status. ([@koic][]) ## 0.91.0 (2020-09-15) diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index cae37dc5a75..cb89705d3ea 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -191,7 +191,16 @@ def alignment_location(alignment_node) end def begin_end_alignment_style - config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith'] + # FIXME: Workaround for pending status for `Layout/BeginEndAlignment` cop + # When RuboCop 1.0 is released, please replace it with the following condition. + # + # config.for_cop('Layout/BeginEndAlignment')['Enabled'] && + # config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith'] + if config.for_all_cops['NewCops'] == 'enable' || + config.for_cop('Layout/BeginEndAlignment')['Enabled'] && + config.for_cop('Layout/BeginEndAlignment')['Enabled'] != 'pending' + config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith'] + end end end end diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 87ae2fd6cc4..7b2f555577a 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -30,6 +30,58 @@ { 'EnforcedStyle' => 'require_parentheses' } end + context '`Layout/BeginEndAlignment` cop is not enabled' do + let(:other_cops) do + { + 'Layout/BeginEndAlignment' => { + 'Enabled' => false, + 'EnforcedStyleAlignWith' => 'start_of_line' + } + } + end + + it 'accepts multi-line, aligned' do + expect_no_offenses(<<~RUBY) + x ||= begin + 1 + rescue + 2 + end + RUBY + end + + it 'accepts multi-line, indented' do + expect_no_offenses(<<~RUBY) + x ||= + begin + 1 + rescue + 2 + end + RUBY + end + + it 'registers an offense and corrects for incorrect alignment' do + expect_offense(<<~RUBY) + x ||= begin + 1 + rescue + ^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6. + 2 + end + RUBY + + # Except for `rescue`, it will be aligned by `Layout/BeginEndAlignment` auto-correction. + expect_correction(<<~RUBY) + x ||= begin + 1 + rescue + 2 + end + RUBY + end + end + context 'when `EnforcedStyleAlignWith: start_of_line` of `Layout/BeginEndAlignment` cop' do let(:other_cops) do {