Skip to content

Commit

Permalink
Fix a false positive for Layout/RedundantLineBreak
Browse files Browse the repository at this point in the history
This PR fixes false positive for `Layout/RedundantLineBreak` to prevent the following infinite loop error:

```console
$ echo 'x def self.y; z end' | be rubocop --stdin example.rb -a --only Layout/RedundantLineBreak,Style/SingleLineMethods
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Layout/RedundantLineBreak: Redundant line break detected.
x def self.y;  ...
^^^^^^^^^^^^^^
example.rb:1:3: C: [Corrected] Style/SingleLineMethods: Avoid single-line method definitions.
x def self.y; z end
  ^^^^^^^^^^^^^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in /Users/koic/src/github.com/rubocop/rubocop/example.rb and caused by Style/SingleLineMethods -> Layout/RedundantLineBreak
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:332:in `check_for_infinite_loop'
```
  • Loading branch information
koic committed Aug 26, 2023
1 parent bea319d commit 90cd056
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 90cd056

Please # to comment.