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 new Lint/ConstantOverwrittenInRescue cop #10722

Merged
merged 1 commit into from Jun 20, 2022
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
@@ -0,0 +1 @@
* [#10722](https://github.com/rubocop/rubocop/pull/10722): Add new `Lint/ConstantOverwrittenInRescue` cop. ([@ydah][])
5 changes: 5 additions & 0 deletions config/default.yml
Expand Up @@ -1561,6 +1561,11 @@ Lint/ConstantDefinitionInBlock:
AllowedMethods:
- enums

Lint/ConstantOverwrittenInRescue:
Description: 'Checks for overwriting an exception with an exception result by use `rescue =>`.'
Enabled: pending
VersionAdded: '<<next>>'

Lint/ConstantResolution:
Description: 'Check that constants are fully qualified with `::`.'
Enabled: false
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -279,6 +279,7 @@
require_relative 'rubocop/cop/lint/boolean_symbol'
require_relative 'rubocop/cop/lint/circular_argument_reference'
require_relative 'rubocop/cop/lint/constant_definition_in_block'
require_relative 'rubocop/cop/lint/constant_overwritten_in_rescue'
require_relative 'rubocop/cop/lint/constant_resolution'
require_relative 'rubocop/cop/lint/debugger'
require_relative 'rubocop/cop/lint/deprecated_class_methods'
Expand Down
51 changes: 51 additions & 0 deletions lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Lint
# Checks for overwriting an exception with an exception result by use `rescue =>`.
#
# You intended to write as `rescue StandardError`.
# However, you have written `rescue => StandardError`.
# In that case, the result of `rescue` will overwrite `StandardError`.
#
# @example
#
# # bad
# begin
# something
# rescue => StandardError
# end
#
# # good
# begin
# something
# rescue StandardError
# end
#
class ConstantOverwrittenInRescue < Base
extend AutoCorrector
include RangeHelp

MSG = '`%<constant>s` is overwritten by `rescue =>`.'

# @!method overwritten_constant(node)
def_node_matcher :overwritten_constant, <<~PATTERN
(resbody nil? (casgn nil? $_) nil?)
PATTERN

def self.autocorrect_incompatible_with
[Naming::RescuedExceptionsVariableName, Style::RescueStandardError]
end

def on_resbody(node)
return unless (constant = overwritten_constant(node))

add_offense(node.loc.assoc, message: format(MSG, constant: constant)) do |corrector|
corrector.remove(range_between(node.loc.keyword.end_pos, node.loc.assoc.end_pos))
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -2518,4 +2518,30 @@ module Foo#{trailing_whitespace}
end
RUBY
end

it 'corrects `Naming/RescuedExceptionsVariableName` and `, `Style/RescueStandardError`' \
'and `Lint/OverwriteByRescue` offenses' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
begin
something
rescue => StandardError
end
RUBY

status = cli.run(
%w[--autocorrect-all --only] << %w[
Naming/RescuedExceptionsVariableName
Style/RescueStandardError
Lint/ConstantOverwrittenInRescue
].join(',')
)
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
begin
something
rescue StandardError
end
RUBY
end
end
38 changes: 38 additions & 0 deletions spec/rubocop/cop/lint/constant_overwritten_in_rescue_spec.rb
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::ConstantOverwrittenInRescue, :config do
it 'registers an offense when overriding an exception with an exception result' do
expect_offense(<<~RUBY)
begin
something
rescue => StandardError
^^ `StandardError` is overwritten by `rescue =>`.
end
RUBY

expect_correction(<<~RUBY)
begin
something
rescue StandardError
end
RUBY
end

it 'does not register an offense when not overriding an exception with an exception result' do
expect_no_offenses(<<~RUBY)
begin
something
rescue StandardError
end
RUBY
end

it 'does not register an offense when using `=>` but correctly assigning to variables' do
expect_no_offenses(<<~RUBY)
begin
something
rescue StandardError => e
end
RUBY
end
end