Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autocorrect for Lint/EmptyEnsure cop #4018

Merged
merged 1 commit into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#3984](https://github.com/bbatsov/rubocop/pull/3984): Add new `Style/EmptyLinesAroundBeginBody` cop. ([@pocke][])
* [#3995](https://github.com/bbatsov/rubocop/pull/3995): Add new `Style/EmptyLinesAroundExceptionHandlingKeywords` cop. ([@pocke][])
* [#4019](https://github.com/bbatsov/rubocop/pull/4019): Make configurable `Style/MultilineMemoization` cop. ([@pocke][])
* [#4018](https://github.com/bbatsov/rubocop/pull/4018): Add autocorrect `Lint/EmptyEnsure` cop. ([@pocke][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ Lint/ElseLayout:
Lint/EmptyEnsure:
Description: 'Checks for empty ensure block.'
Enabled: true
AutoCorrect: false

Lint/EmptyExpression:
Description: 'Checks for empty expressions.'
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/lint/empty_ensure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def on_ensure(node)

add_offense(node, :keyword) unless ensure_body
end

def autocorrect(node)
lambda do |corrector|
corrector.remove(node.loc.keyword)
end
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion manual/cops_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ end

Enabled by default | Supports autocorrection
--- | ---
Enabled | No
Enabled | Yes

This cop checks for empty `ensure` blocks

Expand Down Expand Up @@ -539,6 +539,13 @@ ensure
end
```

### Important attributes

Attribute | Value
--- | ---
AutoCorrect | false


## Lint/EmptyExpression

Enabled by default | Supports autocorrection
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/lint/empty_ensure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
expect(cop.offenses.size).to eq(1)
end

it 'autocorrects for empty ensure' do
corrected = autocorrect_source(cop,
['begin',
' something',
'ensure',
'end'])
expect(corrected).to eq([
'begin',
' something',
'',
'end'
].join("\n"))
end

it 'does not register an offense for non-empty ensure' do
inspect_source(cop,
['begin',
Expand Down