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 negative for Layout/IndentationWidth #9791

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
@@ -0,0 +1 @@
* [#9791](https://github.com/rubocop/rubocop/pull/9791): Fix a false negative for `Layout/IndentationWidth` when using `ensure` in `do` ... `end` block. ([@koic][])
7 changes: 5 additions & 2 deletions lib/rubocop/cop/layout/indentation_width.rb
Expand Up @@ -313,9 +313,12 @@ def indentation_to_check?(base_loc, body_node)
check_rescue?(body_node)
elsif body_node.ensure_type?
block_body, = *body_node
return unless block_body

check_rescue?(block_body) if block_body.rescue_type?
if block_body&.rescue_type?
check_rescue?(block_body)
else
!block_body.nil?
end
else
true
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/layout/indentation_width_spec.rb
Expand Up @@ -1437,6 +1437,27 @@ def bar
RUBY
end

it 'does not register an offense for good indentation of `do` ... `ensure` ... `end` block' do
expect_no_offenses(<<~RUBY)
do_something do
foo
ensure
handle_error
end
RUBY
end

it 'registers an offense for bad indentation of `do` ... `ensure` ... `end` block' do
expect_offense(<<~RUBY)
do_something do
foo
^^^^ Use 2 (not 4) spaces for indentation.
ensure
handle_error
end
RUBY
end

context 'when using safe navigation operator' do
it 'registers an offense for bad indentation of a {} body' do
expect_offense(<<~RUBY)
Expand Down