Showing 1,311 of 1,311 total issues
Use String#include?
instead of a regex match with literal-only pattern. Open
raise unless /not found/.match?(e.message)
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
role['miq_product_feature_ids'] = available_features.collect do |feature|
feature.id if role['feature_identifiers']&.include?(feature.identifier)
end.compact
- Create a ticketCreate a ticket
- Exclude checks
Duplicate branch body detected. Open
elsif respool && host.nil?
:relocate
elsif host
:migrate
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that there are no repeated bodies
within if/unless
, case-when
, case-in
and rescue
constructs.
With IgnoreLiteralBranches: true
, branches are not registered
as offenses if they return a basic literal value (string, symbol,
integer, float, rational, complex, true
, false
, or nil
), or
return an array, hash, regexp or range that only contains one of
the above basic literal values.
With IgnoreConstantBranches: true
, branches are not registered
as offenses if they return a constant value.
Example:
# bad
if foo
do_foo
do_something_else
elsif bar
do_foo
do_something_else
end
# good
if foo || bar
do_foo
do_something_else
end
# bad
case x
when foo
do_foo
when bar
do_foo
else
do_something_else
end
# good
case x
when foo, bar
do_foo
else
do_something_else
end
# bad
begin
do_something
rescue FooError
handle_error
rescue BarError
handle_error
end
# good
begin
do_something
rescue FooError, BarError
handle_error
end
Example: IgnoreLiteralBranches: true
# good
case size
when "small" then 100
when "medium" then 250
when "large" then 1000
else 250
end
Example: IgnoreConstantBranches: true
# good
case size
when "small" then SMALL_SIZE
when "medium" then MEDIUM_SIZE
when "large" then LARGE_SIZE
else MEDIUM_SIZE
end
Avoid more than 3 levels of block nesting. Open
vm.save_drift_state unless vm.nil?
- 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.
Use filter_map
instead. Open
classes.map do |(scopes, (_, name, sklass))|
next unless sklass
scope_names = scopes.map { |s| flatten_name(s) }
search_combos = name_combinations(scope_names)
- Create a ticketCreate a ticket
- Exclude checks
Wrap expressions with varying precedence with parentheses to avoid ambiguity. Open
tag_val1 = tag_val + " " * (@line_len - tag_val.length - 2)
- 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
Wrap expressions with varying precedence with parentheses to avoid ambiguity. Open
last_run_on = mri.rpt_options && mri.rpt_options[:last_run_on] || Time.zone.now
- 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
private
(on line 157) does not make singleton methods private. Use private_class_method
or private
inside a class << self
block instead. Open
def self.ordered_by_desc
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end
private
(on line 157) does not make singleton methods private. Use private_class_method
or private
inside a class << self
block instead. Open
def self.default_time_profile(tz = DEFAULT_TZ)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
[:ae_namespaces, :ae_classes, :ae_instances, :ae_methods].each do |meth|
- Create a ticketCreate a ticket
- Exclude checks
Use find
instead of select.first
. Open
gem_dir = Pathname.new(Bundler.locked_gems.specs.select { |g| g.name == "manageiq-api" }.first.gem_dir)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop is used to identify usages of
select.first
, select.last
, find_all.first
, and find_all.last
and change them to use detect
instead.
Example:
# bad
[].select { |item| true }.first
[].select { |item| true }.last
[].find_all { |item| true }.first
[].find_all { |item| true }.last
# good
[].detect { |item| true }
[].reverse.detect { |item| true }
ActiveRecord
compatibility:
ActiveRecord
does not implement a detect
method and find
has its
own meaning. Correcting ActiveRecord methods with this cop should be
considered unsafe.
Wrap expressions with varying precedence with parentheses to avoid ambiguity. Open
t = customer_name_title + " " * (@line_len - customer_name_title.length - 2)
- 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
Shadowing outer local variable - t
. Open
tables[1..-1].each do |t| # Add on any tables
- 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
private
(on line 157) does not make singleton methods private. Use private_class_method
or private
inside a class << self
block instead. Open
def self.for_user(user_id)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end
Variable new_state
used in void context. Open
new_state
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for operators, variables, literals, lambda, proc and nonmutating methods used in void context.
Example: CheckForMethodsWithNoSideEffects: false (default)
# bad
def some_method
some_num * 10
do_something
end
def some_method(some_var)
some_var
do_something
end
Example: CheckForMethodsWithNoSideEffects: true
# bad
def some_method(some_array)
some_array.sort
do_something(some_array)
end
# good
def some_method
do_something
some_num * 10
end
def some_method(some_var)
do_something
some_var
end
def some_method(some_array)
some_array.sort!
do_something(some_array)
end
Use sum
instead of inject(:+)
, unless calling inject(:+)
on an empty array. Open
avg = 10.times.map { Benchmark.realtime { client.get("test") } }.inject(:+) / 10.0
- Create a ticketCreate a ticket
- Exclude checks
Avoid more than 3 levels of block nesting. Open
if opts[:dry_run]
puts " **** This is a dry-run, nothing updated, skipping. ****"
else
service.add_resource!(matching_stack)
reconnected += 1
- 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 immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
elsif ["<drift>"].include?(mri.db) && r[0] == "Changed:"
- Create a ticketCreate a ticket
- Exclude checks
Use sum((columns.length - 1) * 3)
instead of inject((columns.length - 1) * 3) { |s, e| s + e }
. Open
@line_len = @max_col_width.inject((columns.length - 1) * 3) { |s, e| s + e } + 1
- Create a ticketCreate a ticket
- Exclude checks
Use String#include?
instead of a regex match with literal-only pattern. Open
files.find { |f| f.match(/\/evm\.log/) }
- Create a ticketCreate a ticket
- Exclude checks