Skip to content

Commit

Permalink
Restore Lint/UselessElseWithoutRescue cop
Browse files Browse the repository at this point in the history
This reverts commit 6d0b336 and tweaked.

Follow up #10577 (comment).

RuboCop 1.29.1 and higher can analyze Ruby 2.5 code again, so it can restore the cop.
  • Loading branch information
koic authored and bbatsov committed Jun 7, 2022
1 parent b1ad9bb commit c258452
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Rakefile
Expand Up @@ -95,9 +95,13 @@ task documentation_syntax_check: :yard_for_generate_documentation do
buffer = Parser::Source::Buffer.new('<code>', 1)
buffer.source = example.text

# Ruby 2.7 raises a syntax error in
# `Lint/CircularArgumentReference` cop's example.
parser = if cop == RuboCop::Cop::Lint::CircularArgumentReference
# Ruby 2.6 or higher does not support a syntax used in
# `Lint/UselessElseWithoutRescue` cop's example.
parser = if cop == RuboCop::Cop::Lint::UselessElseWithoutRescue
Parser::Ruby25.new(RuboCop::AST::Builder.new)
# Ruby 2.7 raises a syntax error in
# `Lint/CircularArgumentReference` cop's example.
elsif cop == RuboCop::Cop::Lint::CircularArgumentReference
Parser::Ruby26.new(RuboCop::AST::Builder.new)
# Ruby 3.0 raises a syntax error in
# `Lint/NumberedParameterAssignment` cop's example.
Expand Down
@@ -0,0 +1 @@
* [#10697](https://github.com/rubocop/rubocop/pull/10697): Restore `Lint/UselessElseWithoutRescue` cop. ([@koic][])
6 changes: 6 additions & 0 deletions config/default.yml
Expand Up @@ -2348,6 +2348,12 @@ Lint/UselessAssignment:
Enabled: true
VersionAdded: '0.11'

Lint/UselessElseWithoutRescue:
Description: 'Checks for useless `else` in `begin..end` without `rescue`.'
Enabled: true
VersionAdded: '0.17'
VersionChanged: '<<next>>'

Lint/UselessMethodDefinition:
Description: 'Checks for useless method definitions.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -387,6 +387,7 @@
require_relative 'rubocop/cop/lint/uri_regexp'
require_relative 'rubocop/cop/lint/useless_access_modifier'
require_relative 'rubocop/cop/lint/useless_assignment'
require_relative 'rubocop/cop/lint/useless_else_without_rescue'
require_relative 'rubocop/cop/lint/useless_method_definition'
require_relative 'rubocop/cop/lint/useless_ruby2_keywords'
require_relative 'rubocop/cop/lint/useless_setter_call'
Expand Down
44 changes: 44 additions & 0 deletions lib/rubocop/cop/lint/useless_else_without_rescue.rb
@@ -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
30 changes: 30 additions & 0 deletions spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb
@@ -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

0 comments on commit c258452

Please sign in to comment.