Skip to content

Commit

Permalink
Fix autocorrection of trivial body for `Naming/RescuedExceptionsVaria…
Browse files Browse the repository at this point in the history
…bleName` (#9398)
  • Loading branch information
asterite authored Jan 21, 2021
1 parent 270eff8 commit 249e2c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Bug fixes

* [#7766](https://github.com/rubocop-hq/rubocop/issues/7766): Rename rescue body vars when renaming exception name. ([@asterite][])
* [#9342](https://github.com/rubocop-hq/rubocop/issues/9342): Fix an error for `Lint/RedundantDirGlobSort` when using `collection.sort`. ([@koic][])
* [#9304](https://github.com/rubocop-hq/rubocop/issues/9304): Do not register an offense for `Style/ExplicitBlockArgument` when the `yield` arguments are not an exact match with the block arguments. ([@dvandersluis][])
* [#8281](https://github.com/rubocop-hq/rubocop/issues/8281): Fix Style/WhileUntilModifier handling comments and assignment when correcting to modifier form. ([@Darhazer][])
Expand Down Expand Up @@ -5367,3 +5368,4 @@
[@magneland]: https://github.com/magneland
[@k-karen]: https://github.com/k-karen
[@uplus]: https://github.com/uplus
[@asterite]: https://github.com/asterite
2 changes: 1 addition & 1 deletion lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def on_resbody(node)
add_offense(range, message: message) do |corrector|
corrector.replace(range, preferred_name)

node.body&.each_descendant(:lvar) do |var|
node.body&.each_node(:lvar) do |var|
next unless var.children.first == offending_name

corrector.replace(var, preferred_name)
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/naming/rescued_exceptions_variable_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@
RUBY
end

it 'registers an offense when using `exc` and renames its usage' do
expect_offense(<<~RUBY)
begin
something
rescue MyException => exc
^^^ Use `e` instead of `exc`.
exc
end
RUBY

expect_correction(<<~RUBY)
begin
something
rescue MyException => e
e
end
RUBY
end

it 'registers offenses when using `foo` and `bar` ' \
'in multiple rescues' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 249e2c5

Please sign in to comment.