Skip to content

Commit

Permalink
[Fix rubocop#4740] Make Lint/RescueWithoutErrorClass aware of modif…
Browse files Browse the repository at this point in the history
…ier form `rescue`

The cop would miss cases of `rescue` without error class when used in
modifier form.

This change fixes that.
  • Loading branch information
Drenmi committed Sep 15, 2017
1 parent 6566745 commit df872c5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#4740](https://github.com/bbatsov/rubocop/issues/4740): Make `Lint/RescueWithoutErrorClass` aware of modifier form `rescue`. ([@drenmi][])

## 0.50.0 (2017-09-14)

### New features
Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/lint/rescue_without_error_class.rb
Expand Up @@ -20,11 +20,19 @@ module Lint
# rescue
# bar
# end
#
# @example
#
# # good
# foo rescue BarError; "baz"
#
# # bad
# foo rescue "baz"
class RescueWithoutErrorClass < Cop
MSG = 'Avoid rescuing without specifying an error class.'.freeze

def_node_matcher :rescue_without_error_class?, <<-PATTERN
(resbody nil ...)
(resbody nil _ !(const ...))
PATTERN

def on_resbody(node)
Expand Down
7 changes: 7 additions & 0 deletions manual/cops_lint.md
Expand Up @@ -1592,6 +1592,13 @@ rescue
bar
end
```
```ruby
# good
foo rescue BarError; "baz"

# bad
foo rescue "baz"
```

### References

Expand Down
59 changes: 59 additions & 0 deletions spec/rubocop/cop/lint/rescue_without_error_class_spec.rb
Expand Up @@ -15,6 +15,17 @@
RUBY
end

it 'registers an offense without an error class, assigning to variable' do
expect_offense(<<-RUBY.strip_indent)
begin
foo
rescue => e
^^^^^^ Avoid rescuing without specifying an error class.
bar
end
RUBY
end

it 'does not register an offense with an error class' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
Expand All @@ -24,6 +35,17 @@
end
RUBY
end

it 'does not register an offense with an error class, ' \
'assigning to variable' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
foo
rescue BarError => e
bar
end
RUBY
end
end

context 'when rescuing in a method definition' do
Expand All @@ -38,6 +60,17 @@ def baz
RUBY
end

it 'registers an offense without an error class, assigning to variable' do
expect_offense(<<-RUBY.strip_indent)
def baz
foo
rescue => e
^^^^^^ Avoid rescuing without specifying an error class.
bar
end
RUBY
end

it 'does not register an offense with an error class' do
expect_no_offenses(<<-RUBY.strip_indent)
def baz
Expand All @@ -47,5 +80,31 @@ def baz
end
RUBY
end

it 'does not register an offense with an error class, ' \
'assigning to variable' do
expect_no_offenses(<<-RUBY.strip_indent)
def baz
foo
rescue BarError => e
bar
end
RUBY
end
end

context 'when rescuing as a modifier' do
it 'registers an offense with something besides an an error class' do
expect_offense(<<-RUBY.strip_indent)
foo rescue 42
^^^^^^ Avoid rescuing without specifying an error class.
RUBY
end

it 'does not register an offense with an error class' do
expect_no_offenses(<<-RUBY.strip_indent)
foo rescue BarError
RUBY
end
end
end

0 comments on commit df872c5

Please sign in to comment.