Skip to content

Commit

Permalink
Change AllowComments of Lint/SuppressedException to true by default
Browse files Browse the repository at this point in the history
## Summary

Follow #7052 (comment).

This PR changes `AllowComments` option of `Lint/SuppressedException` to
true by default.

As is used in RuboCop repo itself, it is a common practice to use
source code comments to explain when exception handling is not performed.

## Related Information

I think that `Suppressing Exceptions` rule can be refined to a suitable example.
https://github.com/rubocop-hq/ruby-style-guide#dont-hide-exceptions
  • Loading branch information
koic authored and bbatsov committed Mar 19, 2020
1 parent bf9fc25 commit 84d6c57
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
### Changes

* [#7797](https://github.com/rubocop-hq/rubocop/pull/7797): Allow unicode-display_width dependency version 1.7.0. ([@yuritomanek][])
* [#7779](https://github.com/rubocop-hq/rubocop/issues/7779): Change `AllowComments` option of `Lint/SuppressedException` to true by default. ([@koic][])

## 0.80.1 (2020-02-29)

Expand Down
4 changes: 2 additions & 2 deletions config/default.yml
Expand Up @@ -1712,9 +1712,9 @@ Lint/SuppressedException:
Description: "Don't suppress exceptions."
StyleGuide: '#dont-hide-exceptions'
Enabled: true
AllowComments: false
AllowComments: true
VersionAdded: '0.9'
VersionChanged: '0.77'
VersionChanged: '0.81'

Lint/Syntax:
Description: 'Checks syntax error.'
Expand Down
34 changes: 12 additions & 22 deletions lib/rubocop/cop/lint/suppressed_exception.rb
Expand Up @@ -5,7 +5,7 @@ module Cop
module Lint
# This cop checks for *rescue* blocks with no body.
#
# @example AllowComments: false (default)
# @example
#
# # bad
# def some_method
Expand All @@ -14,25 +14,11 @@ module Lint
# end
#
# # bad
# def some_method
# do_something
# rescue
# # do nothing
# end
#
# # bad
# begin
# do_something
# rescue
# end
#
# # bad
# begin
# do_something
# rescue
# # do nothing
# end
#
# # good
# def some_method
# do_something
Expand All @@ -47,32 +33,36 @@ module Lint
# handle_exception
# end
#
# @example AllowComments: true
# @example AllowComments: true (default)
#
# # bad
# # good
# def some_method
# do_something
# rescue
# # do nothing
# end
#
# # bad
# # good
# begin
# do_something
# rescue
# # do nothing
# end
#
# # good
# @example AllowComments: false
#
# # bad
# def some_method
# do_something
# rescue
# # do nothing but comment
# # do nothing
# end
#
# # good
# # bad
# begin
# do_something
# rescue
# # do nothing but comment
# # do nothing
# end
class SuppressedException < Cop
MSG = 'Do not suppress exceptions.'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/formatter/clang_style_formatter.rb
Expand Up @@ -29,7 +29,7 @@ def report_offense(file, offense)

report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
rescue IndexError # rubocop:disable Lint/SuppressedException
rescue IndexError
# range is not on a valid line; perhaps the source file is empty
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/formatter/tap_formatter.rb
Expand Up @@ -56,7 +56,7 @@ def report_offense(file, offense)

report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
rescue IndexError # rubocop:disable Lint/SuppressedException
rescue IndexError
# range is not on a valid line; perhaps the source file is empty
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/processed_source.rb
Expand Up @@ -163,7 +163,7 @@ def tokenize(parser)
ast, comments, tokens = parser.tokenize(@buffer)

ast.respond_to?(:complete!) && ast.complete!
rescue Parser::SyntaxError # rubocop:disable Lint/SuppressedException
rescue Parser::SyntaxError
# All errors are in diagnostics. No need to handle exception.
end

Expand Down
39 changes: 14 additions & 25 deletions manual/cops_lint.md
Expand Up @@ -2279,41 +2279,25 @@ end

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

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

# bad
begin
do_something
rescue
end

# bad
begin
do_something
rescue
# do nothing
end

# good
def some_method
do_something
Expand All @@ -2328,41 +2312,46 @@ rescue
handle_exception
end
```
#### AllowComments: true
#### AllowComments: true (default)

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

# bad
# good
begin
do_something
rescue
# do nothing
end
```
#### AllowComments: false

# good
```ruby
# bad
def some_method
do_something
rescue
# do nothing but comment
# do nothing
end

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

### Configurable attributes

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

### References

Expand Down

0 comments on commit 84d6c57

Please sign in to comment.