Skip to content

Commit

Permalink
[Fix rubocop#10939] Fix an error for Style/Next
Browse files Browse the repository at this point in the history
Fixes rubocop#10939.

This PR fixes an error for `Style/Next` when line break before condition.
The removed indentation logic can be delegated to `Layout/IndentationConsistency`.
  • Loading branch information
koic committed Aug 21, 2022
1 parent 23a3924 commit 8559c57
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_next.md
@@ -0,0 +1 @@
* [#10939](https://github.com/rubocop/rubocop/issues/10939): Fix an error for `Style/Next` when line break before condition. ([@koic][])
6 changes: 1 addition & 5 deletions lib/rubocop/cop/style/next.rb
Expand Up @@ -225,11 +225,7 @@ def reindent_line(corrector, lineno, delta, buffer)
adjustment = delta + @reindented_lines[lineno]
@reindented_lines[lineno] = adjustment

if adjustment.positive?
corrector.remove_leading(buffer.line_range(lineno), adjustment)
elsif adjustment.negative?
corrector.insert_before(buffer.line_range(lineno), ' ' * -adjustment)
end
corrector.remove_leading(buffer.line_range(lineno), adjustment) if adjustment.positive?
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/style/next_spec.rb
Expand Up @@ -315,6 +315,27 @@
RUBY
end

it 'registers an offense when line break before condition' do
expect_offense(<<~RUBY)
array.each do |item|
if
^^ Use `next` to skip iteration.
condition
next if item.zero?
do_something
end
end
RUBY

expect_correction(<<~RUBY)
array.each do |item|
next unless condition
next if item.zero?
do_something
end
RUBY
end

it 'allows loops with conditional break' do
expect_no_offenses(<<~RUBY)
loop do
Expand Down

0 comments on commit 8559c57

Please sign in to comment.