Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Layout/RedundantLineBreak #12155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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