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

Restore Lint/UselessElseWithoutRescue cop #10697

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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