forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore
Lint/UselessElseWithoutRescue
cop
This reverts commit 6d0b336 and tweaked. Follow up rubocop#10577 (comment). RuboCop 1.29.1 and higher can analyze Ruby 2.5 code again, so it can restore the cop.
- Loading branch information
Showing
6 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
changelog/change_restore_lint_useless_else_without_rescue_cop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10697](https://github.com/rubocop/rubocop/pull/10697): Restore `Lint/UselessElseWithoutRescue` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# Checks for useless `else` in `begin..end` without `rescue`. | ||
# | ||
# NOTE: This syntax is no longer valid on Ruby 2.6 or higher. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# | ||
# begin | ||
# do_something | ||
# else | ||
# do_something_else # This will never be run. | ||
# end | ||
# | ||
# @example | ||
# | ||
# # good | ||
# | ||
# begin | ||
# do_something | ||
# rescue | ||
# handle_errors | ||
# else | ||
# do_something_else | ||
# end | ||
class UselessElseWithoutRescue < Base | ||
MSG = '`else` without `rescue` is useless.' | ||
|
||
def on_new_investigation | ||
processed_source.diagnostics.each do |diagnostic| | ||
next unless diagnostic.reason == :useless_else | ||
|
||
add_offense(diagnostic.location, severity: diagnostic.level) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::UselessElseWithoutRescue, :config do | ||
context 'with `else` without `rescue`', :ruby25 do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
begin | ||
do_something | ||
else | ||
^^^^ `else` without `rescue` is useless. | ||
handle_unknown_errors | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `else` with `rescue`' do | ||
it 'accepts' do | ||
expect_no_offenses(<<~RUBY) | ||
begin | ||
do_something | ||
rescue ArgumentError | ||
handle_argument_error | ||
else | ||
handle_unknown_errors | ||
end | ||
RUBY | ||
end | ||
end | ||
end |