Use e
instead of ex
. Open
rescue => ex
- Read upRead up
- Exclude checks
Makes sure that rescued exceptions variables are named as expected.
The PreferredName
config option takes a String
. It represents
the required name of the variable. Its default is e
.
NOTE: This cop does not consider nested rescues because it cannot guarantee that the variable from the outer rescue is not used within the inner rescue (in which case, changing the inner variable would shadow the outer variable).
Example: PreferredName: e (default)
# bad
begin
# do something
rescue MyException => exception
# do something
end
# good
begin
# do something
rescue MyException => e
# do something
end
# good
begin
# do something
rescue MyException => _e
# do something
end
Example: PreferredName: exception
# bad
begin
# do something
rescue MyException => e
# do something
end
# good
begin
# do something
rescue MyException => exception
# do something
end
# good
begin
# do something
rescue MyException => _exception
# do something
end
Avoid rescuing without specifying an error class. Open
rescue => ex
- Read upRead up
- Exclude checks
Checks for rescuing StandardError
. There are two supported
styles implicit
and explicit
. This cop will not register an offense
if any error other than StandardError
is specified.
Example: EnforcedStyle: implicit
# `implicit` will enforce using `rescue` instead of
# `rescue StandardError`.
# bad
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Example: EnforcedStyle: explicit (default)
# `explicit` will enforce using `rescue StandardError`
# instead of `rescue`.
# bad
begin
foo
rescue
bar
end
# good
begin
foo
rescue StandardError
bar
end
# good
begin
foo
rescue OtherError
bar
end
# good
begin
foo
rescue StandardError, SecurityError
bar
end
Prefer ary[n..] over ary[n..-1]. Open
Array(@args[1..-1])
- Read upRead up
- Exclude checks
Checks that arrays are sliced with endless ranges instead of
ary[start..-1]
on Ruby 2.6+.
Safety:
This cop is unsafe because x..-1
and x..
are only guaranteed to
be equivalent for Array#[]
, and the cop cannot determine what class
the receiver is.
For example:
ruby
sum = proc { |ary| ary.sum }
sum[-3..-1] # => -6
sum[-3..] # Hangs forever
Example:
# bad
items[1..-1]
# good
items[1..]