Showing 218 of 218 total issues
Useless assignment to variable - err
. Open
rescue NoSuchFileOrDirectory => err
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
Use @settings[:uri] = File.dirname(File.dirname(log_uri))
instead of @settings.merge!(:uri => File.dirname(File.dirname(log_uri)))
. Open
@settings.merge!(:uri => File.dirname(File.dirname(log_uri)))
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop identifies places where Hash#merge!
can be replaced by
Hash#[]=
.
Example:
hash.merge!(a: 1)
hash.merge!({'key' => 'value'})
hash.merge!(a: 1, b: 2)
Odd else
layout detected. Did you mean to use elsif
? Open
else break
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for odd else
block layout - like
having an expression on the same line as the else
keyword,
which is usually a mistake.
Its autocorrection tweaks layout to keep the syntax. So, this autocorrection
is compatible correction for bad case syntax, but if your code makes a mistake
with elsif
and else
, you will have to correct it manually.
Example:
# bad
if something
# ...
else do_this
do_that
end
Example:
# good
# This code is compatible with the bad case. It will be autocorrected like this.
if something
# ...
else
do_this
do_that
end
# This code is incompatible with the bad case.
# If `do_this` is a condition, `elsif` should be used instead of `else`.
if something
# ...
elsif do_this
do_that
end
Shadowing outer local variable - h
. Open
h.each_with_object({}) { |(k, v), h| h[k.to_s] = remove_invalid_chars(v.to_s.encode('UTF-8', :undef => :replace, :invalid => :replace, :replace => '')) }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for the use of local variable names from an outer scope
in block arguments or block-local variables. This mirrors the warning
given by ruby -cw
prior to Ruby 2.6:
"shadowing outer local variable - foo".
NOTE: Shadowing of variables in block passed to Ractor.new
is allowed
because Ractor
should not access outer variables.
eg. following style is encouraged:
```ruby
worker_id, pipe = env
Ractor.new(worker_id, pipe) do |worker_id, pipe|
end
```
Example:
# bad
def some_method
foo = 1
2.times do |foo| # shadowing outer `foo`
do_something(foo)
end
end
Example:
# good
def some_method
foo = 1
2.times do |bar|
do_something(bar)
end
end
Useless assignment to variable - level
. Open
level += 2
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
Wrap expressions with varying precedence with parentheses to avoid ambiguity. Open
nt_time = nt_time / 10_000_000 - 11_644_495_200
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Looks for expressions containing multiple binary operators
where precedence is ambiguous due to lack of parentheses. For example,
in 1 + 2 * 3
, the multiplication will happen before the addition, but
lexically it appears that the addition will happen first.
The cop does not consider unary operators (ie. !a
or -b
) or comparison
operators (ie. a =~ b
) because those are not ambiguous.
NOTE: Ranges are handled by Lint/AmbiguousRange
.
Example:
# bad
a + b * c
a || b && c
a ** b + c
# good (different precedence)
a + (b * c)
a || (b && c)
(a ** b) + c
# good (same precedence)
a + b + c
a * b / c % d
Avoid more than 3 levels of block nesting. Open
$log.error "Encoding error: #{second_utf8}" if $log
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Avoid when
branches without a body. Open
when *REXML::Text::VALID_CHAR
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for the presence of when
branches without a body.
Example:
# bad
case foo
when bar
do_something
when baz
end
Example:
# good
case condition
when foo
do_something
when bar
nil
end
Example: AllowComments: true (default)
# good
case condition
when foo
do_something
when bar
# noop
end
Example: AllowComments: false
# bad
case condition
when foo
do_something
when bar
# do nothing
end
Useless assignment to variable - opaque
. Use _
or _opaque
as a variable name to indicate that it won't be used. Open
scheme, userinfo, @host, port, registry, @mount_root, opaque, query, fragment = URI.split(URI::DEFAULT_PARSER.escape(@settings[:uri]))
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
Use match?
instead of =~
when MatchData
is not used. Open
unless err.message =~ /not found|mounted/
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
In Ruby 2.4, String#match?
, Regexp#match?
and Symbol#match?
have been added. The methods are faster than match
.
Because the methods avoid creating a MatchData
object or saving
backref.
So, when MatchData
is not used, use match?
instead of match
.
Example:
# bad
def foo
if x =~ /re/
do_something
end
end
# bad
def foo
if x.match(/re/)
do_something
end
end
# bad
def foo
if /re/ === x
do_something
end
end
# good
def foo
if x.match?(/re/)
do_something
end
end
# good
def foo
if x =~ /re/
do_something(Regexp.last_match)
end
end
# good
def foo
if x.match(/re/)
do_something($~)
end
end
# good
def foo
if /re/ === x
do_something($~)
end
end
Use String#include?
instead of a regex match with literal-only pattern. Open
if err.kind_of?(RuntimeError) && err.message =~ /No such file or directory/
- Create a ticketCreate a ticket
- Exclude checks
Useless assignment to variable - err
. Open
rescue LoadError => err
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
Useless assignment to variable - opaque
. Use _
or _opaque
as a variable name to indicate that it won't be used. Open
scheme, userinfo, host, port, registry, share, opaque, query, fragment = URI.split(URI::DEFAULT_PARSER.escape(@settings[:uri]))
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
Check block argument explicitly instead of using block_given?
. Open
raise "requires a block" unless block_given?
- Create a ticketCreate a ticket
- Exclude checks
Useless assignment to variable - i
. Open
i = 0
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
This loop will have at most one iteration. Open
each_element { |_e| return true }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for loops that will have at most one iteration.
A loop that can never reach the second iteration is a possible error in the code.
In rare cases where only one iteration (or at most one iteration) is intended behavior,
the code should be refactored to use if
conditionals.
NOTE: Block methods that are used with Enumerable
s are considered to be loops.
AllowedPatterns
can be used to match against the block receiver in order to allow
code that would otherwise be registered as an offense (eg. times
used not in an
Enumerable
context).
Example:
# bad
while node
do_something(node)
node = node.parent
break
end
# good
while node
do_something(node)
node = node.parent
end
# bad
def verify_list(head)
item = head
begin
if verify(item)
return true
else
return false
end
end while(item)
end
# good
def verify_list(head)
item = head
begin
if verify(item)
item = item.next
else
return false
end
end while(item)
true
end
# bad
def find_something(items)
items.each do |item|
if something?(item)
return item
else
raise NotFoundError
end
end
end
# good
def find_something(items)
items.each do |item|
if something?(item)
return item
end
end
raise NotFoundError
end
# bad
2.times { raise ArgumentError }
Example: AllowedPatterns: ['(exactly|atleast|atmost)(\d+).times'] (default)
# good
exactly(2).times { raise StandardError }
Avoid elsif
branches without a body. Open
elsif object.respond_to?(:to_xml)
else
raise "Invalid child object type [#{object.class}] for XmlHash"
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for the presence of if
, elsif
and unless
branches without a body.
NOTE: empty else
branches are handled by Style/EmptyElse
.
Safety:
Autocorrection for this cop is not safe. The conditions for empty branches that the autocorrection removes may have side effects, or the logic in subsequent branches may change due to the removal of a previous condition.
Example:
# bad
if condition
end
# bad
unless condition
end
# bad
if condition
do_something
elsif other_condition
end
# good
if condition
do_something
end
# good
unless condition
do_something
end
# good
if condition
do_something
elsif other_condition
do_something_else
end
Example: AllowComments: true (default)
# good
if condition
do_something
elsif other_condition
# noop
end
Example: AllowComments: false
# bad
if condition
do_something
elsif other_condition
# noop
end
Useless assignment to variable - fragment
. Use _
or _fragment
as a variable name to indicate that it won't be used. Open
scheme, userinfo, host, port, registry, path, opaque, query, fragment = URI.split(URI::DEFAULT_PARSER.escape(uri))
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end