Skip to content

Commit

Permalink
Add AllowComments option to Lint/HandleExceptions
Browse files Browse the repository at this point in the history
Issue: rubocop#7052

To allow empty rescue blocks with comments
  • Loading branch information
tejasbubane committed May 20, 2019
1 parent c89485f commit ae609b8
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#6649](https://github.com/rubocop-hq/rubocop/pull/6649): `Layout/AlignHash` supports list of options. ([@stoivo][])
* Add `IgnoreMethodPatterns` config option to `Style/MethodCallWithArgsParentheses`. ([@tejasbubane][])
* [#7052](https://github.com/rubocop-hq/rubocop/issues/7052): Add `AllowComments` option to ` Lint/HandleExceptions`. ([@tejasbubane][])

### Bug fixes

Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Expand Up @@ -1360,7 +1360,9 @@ Lint/HandleExceptions:
Description: "Don't suppress exception."
StyleGuide: '#dont-hide-exceptions'
Enabled: true
AllowComments: false
VersionAdded: '0.9'
VersionChanged: '0.70'

Lint/HeredocMethodCallPosition:
Description: >-
Expand Down
55 changes: 47 additions & 8 deletions lib/rubocop/cop/lint/handle_exceptions.rb
Expand Up @@ -5,50 +5,89 @@ module Cop
module Lint
# This cop checks for *rescue* blocks with no body.
#
# @example
# @example AllowComments: false (default)
#
# # bad
# def some_method
# do_something
# rescue
# end
#
# # bad
# def some_method
# do_something
# rescue
# # do nothing
# end
#
# @example
#
# # bad
# begin
# do_something
# rescue
# end
#
# # bad
# begin
# do_something
# rescue
# # do nothing
# end
#
# @example
# # good
# def some_method
# do_something
# rescue
# handle_exception
# end
#
# # good
# begin
# do_something
# rescue
# handle_exception
# end
#
# @example AllowComments: true
#
# # bad
# def some_method
# do_something
# rescue
# handle_exception
# end
#
# @example
# # bad
# begin
# do_something
# rescue
# end
#
# # good
# def some_method
# do_something
# rescue
# # do nothing but comment
# end
#
# # good
# begin
# do_something
# rescue
# handle_exception
# # do nothing but comment
# end
class HandleExceptions < Cop
MSG = 'Do not suppress exceptions.'

def on_resbody(node)
add_offense(node) unless node.body
return if node.body
return if cop_config['AllowComments'] && comment_lines?(node)

add_offense(node)
end

private

def comment_lines?(node)
processed_source[line_range(node)].any? { |line| comment_line?(line) }
end
end
end
Expand Down
56 changes: 49 additions & 7 deletions manual/cops_lint.md
Expand Up @@ -809,49 +809,91 @@ format('A value: %s and another: %i', a_value, another)

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.9 | -
Enabled | Yes | No | 0.9 | 0.70

This cop checks for *rescue* blocks with no body.

### Examples

#### AllowComments: false (default)

```ruby
# bad
def some_method
do_something
rescue
end

# bad
def some_method
do_something
rescue
# do nothing
end
```
```ruby

# bad
begin
do_something
rescue
end

# bad
begin
do_something
rescue
# do nothing
end
```
```ruby
# good

# good
def some_method
do_something
rescue
handle_exception
end

# good
begin
do_something
rescue
handle_exception
end
```
#### AllowComments: true

```ruby
# bad
def some_method
do_something
rescue
end

# bad
begin
do_something
rescue
end

# good
def some_method
do_something
rescue
# do nothing but comment
end

# good
begin
do_something
rescue
handle_exception
# do nothing but comment
end
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
AllowComments | `false` | Boolean

### References

* [https://github.com/rubocop-hq/ruby-style-guide#dont-hide-exceptions](https://github.com/rubocop-hq/ruby-style-guide#dont-hide-exceptions)
Expand Down
19 changes: 17 additions & 2 deletions spec/rubocop/cop/lint/handle_exceptions_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::HandleExceptions do
subject(:cop) { described_class.new }
RSpec.describe RuboCop::Cop::Lint::HandleExceptions, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense for empty rescue block' do
expect_offense(<<~RUBY)
Expand All @@ -24,4 +24,19 @@
end
RUBY
end

context 'AllowComments' do
let(:cop_config) { { 'AllowComments' => true } }

it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
begin
something
return
rescue
# do nothing
end
RUBY
end
end
end

0 comments on commit ae609b8

Please sign in to comment.