Skip to content

Commit

Permalink
Merge pull request #12155 from koic/fix_a_false_positive_for_layout_r…
Browse files Browse the repository at this point in the history
…edundant_line_break

Fix a false positive for `Layout/RedundantLineBreak`
  • Loading branch information
koic committed Aug 27, 2023
2 parents 4f46bc4 + 90cd056 commit 0e1d4da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12155](https://github.com/rubocop/rubocop/pull/12155): Fix false positive for `Layout/RedundantLineBreak` when using a modified singleton method definition. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/redundant_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def single_line_block_chain_enabled?

def suitable_as_single_line?(node)
!comment_within?(node) &&
node.each_descendant(:if, :case, :kwbegin, :def).none? &&
node.each_descendant(:if, :case, :kwbegin, :def, :defs).none? &&
node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
node.each_descendant(:begin).none? { |b| !b.single_line? }
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,30 @@ def do_even_more_stuff
RUBY
end

it 'corrects `Layout/RedundantLineBreak` and `Style/SingleLineMethods` offenses' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
TargetRubyVersion: 2.7
Layout/RedundantLineBreak:
Enabled: true
Layout/SingleLineMethods:
Enabled: true
YAML

source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
x def self.y; z end
RUBY

expect(cli.run(['-a', '--only', 'Layout/RedundantLineBreak,Style/SingleLineMethods'])).to eq(0)

expect(source_file.read).to eq(<<~RUBY)
x def self.y;#{' '}
z#{' '}
end
RUBY
end

it 'corrects `Layout/DotPosition` and `Layout/SingleLineBlockChain` offenses' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
RUBY
end

it 'accepts a modified singleton method definition' do
expect_no_offenses(<<~RUBY)
x def self.y
z
end
RUBY
end

it 'accepts a method call on a single line' do
expect_no_offenses(<<~RUBY)
my_method(1, 2, "x")
Expand Down

0 comments on commit 0e1d4da

Please sign in to comment.