diff --git a/CHANGELOG.md b/CHANGELOG.md index add48797ac6..ac4a3584441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#8124](https://github.com/rubocop-hq/rubocop/issues/8124): Fix a false positive for `Lint/FormatParameterMismatch` when using named parameters with escaped `%`. ([@koic][]) * [#7979](https://github.com/rubocop-hq/rubocop/issues/7979): Fix "uninitialized constant DidYouMean::SpellChecker" exception. ([@bquorning][]) * [#8098](https://github.com/rubocop-hq/rubocop/issues/8098): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using interpolations. ([@owst][]) +* [#8150](https://github.com/rubocop-hq/rubocop/pull/8150): Fix a false positive for `Layout/EmptyLinesAroundAttributeAccessor` when using attribute accessors in `if` ... `else` branches. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb b/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb index 9c435c63fbe..999024c4da2 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb @@ -96,6 +96,8 @@ def require_empty_line?(node) end def next_line_node(node) + return if node.parent.if_type? + node.parent.children[node.sibling_index + 1] end diff --git a/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb index 5158f3c5bf4..a6a0402a9ba 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb @@ -75,6 +75,16 @@ class Foo RUBY end + it 'does not registers an offense and corrects when using `if` ... `else` branches' do + expect_no_offenses(<<~RUBY) + if condition + attr_reader :foo + else + do_something + end + RUBY + end + context 'when `AllowAliasSyntax: true`' do let(:cop_config) do { 'AllowAliasSyntax' => true }