Skip to content

Commit

Permalink
Merge pull request #9791 from koic/fix_false_negative_for_layout_inde…
Browse files Browse the repository at this point in the history
…ntation_width

Fix a false negative for `Layout/IndentationWidth`
  • Loading branch information
koic committed May 11, 2021
2 parents 45f5a20 + b37720d commit 9c92575
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
@@ -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

0 comments on commit 9c92575

Please sign in to comment.