diff --git a/Rakefile b/Rakefile index 60df9eba6a1..1336bb1deb9 100644 --- a/Rakefile +++ b/Rakefile @@ -95,9 +95,13 @@ task documentation_syntax_check: :yard_for_generate_documentation do buffer = Parser::Source::Buffer.new('', 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. diff --git a/changelog/change_restore_lint_useless_else_without_rescue_cop.md b/changelog/change_restore_lint_useless_else_without_rescue_cop.md new file mode 100644 index 00000000000..ac810e1994c --- /dev/null +++ b/changelog/change_restore_lint_useless_else_without_rescue_cop.md @@ -0,0 +1 @@ +* [#10697](https://github.com/rubocop/rubocop/pull/10697): Restore `Lint/UselessElseWithoutRescue` cop. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index 2587bc45381..60f1c4b39eb 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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: '<>' + Lint/UselessMethodDefinition: Description: 'Checks for useless method definitions.' Enabled: true diff --git a/lib/rubocop.rb b/lib/rubocop.rb index 7ef54eaa598..27cd9c57bbe 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -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' diff --git a/lib/rubocop/cop/lint/useless_else_without_rescue.rb b/lib/rubocop/cop/lint/useless_else_without_rescue.rb new file mode 100644 index 00000000000..966c27d1a95 --- /dev/null +++ b/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 diff --git a/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb b/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb new file mode 100644 index 00000000000..eecb419c92a --- /dev/null +++ b/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