Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/ConstantOverwrittenInRescue
cop
This PR is add a new cop. 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 ```ruby # bad begin something rescue => StandardError end # good begin something rescue StandardError end ```
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10722](https://github.com/rubocop/rubocop/pull/10722): Add new `Lint/ConstantOverwrittenInRescue` cop. ([@ydah][]) |
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,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 |
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
38 changes: 38 additions & 0 deletions
38
spec/rubocop/cop/lint/constant_overwritten_in_rescue_spec.rb
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,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 |