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 #10939] Fix an error for Style/Next #10942

Merged
merged 1 commit into from Aug 22, 2022
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
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