Showing 1,311 of 1,311 total issues
Use filter_map
instead. Open
miq_provisions.collect(&:vm).compact
- Create a ticketCreate a ticket
- Exclude checks
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
next unless [:bytes_human_precision_2, :kilobytes_human, :megabytes_human, :megabytes_human_precision_2, :gigabytes_human, :gigabytes_human_precision_2].include?(format)
- Create a ticketCreate a ticket
- Exclude checks
Use inputs['EventStream::event_stream'] = event_obj.id; inputs[:event_stream_id] = event_obj.id
instead of inputs.merge!('EventStream::event_stream' => event_obj.id, :event_stream_id => event_obj.id)
. Open
inputs.merge!('EventStream::event_stream' => event_obj.id, :event_stream_id => event_obj.id)
- 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)
Use StandardError
over Exception
. Open
raise Exception, _("Region [%{region_id}] does not match the database's region [%{db_id}]") % {:region_id => my_region_id, :db_id => db_region_id}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for raise
or fail
statements which are
raising Exception
class.
You can specify a module name that will be an implicit namespace
using AllowedImplicitNamespaces
option. The cop cause a false positive
for namespaced Exception
when a namespace is omitted. This option can
prevent the false positive by specifying a namespace to be omitted for
Exception
. Alternatively, make Exception
a fully qualified class
name with an explicit namespace.
Safety:
This cop is unsafe because it will change the exception class being raised, which is a change in behavior.
Example:
# bad
raise Exception, 'Error message here'
# good
raise StandardError, 'Error message here'
Example: AllowedImplicitNamespaces: ['Gem']
# good
module Gem
def self.foo
raise Exception # This exception means `Gem::Exception`.
end
end
Use filter_map
instead. Open
when :datetime then Time.at(@table.data.collect { |d| d.data[sb] }.compact.max.to_i + 1).utc
- Create a ticketCreate a ticket
- Exclude checks
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
next if %w[name default_value substitute].include?(cname)
- Create a ticketCreate a ticket
- Exclude checks
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
next if %w[name language scope location data].include?(cname)
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
filtered_result = result.collect do |rec|
rec if get_sub_key_values(rec, sub_key).include?(sub_key_value.downcase)
end.compact
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
miq_policy_contents.where(:miq_event_definition => event).order(order).collect do |pe|
next unless pe.qualifier == on.to_s
pe.get_action(on)
end.compact
- Create a ticketCreate a ticket
- Exclude checks
Duplicate branch body detected. Open
when "notifier"
options[:role] = service
- 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
Do not return
in begin..end
blocks in assignment contexts. Open
return if options.nil?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for the presence of a return
inside a begin..end
block
in assignment contexts.
In this situation, the return
will result in an exit from the current
method, possibly leading to unexpected behavior.
Example:
# bad
@some_variable ||= begin
return some_value if some_condition_is_met
do_something
end
Example:
# good
@some_variable ||= begin
if some_condition_is_met
some_value
else
do_something
end
end
# good
some_variable = if some_condition_is_met
return if another_condition_is_met
some_value
else
do_something
end
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
if ["categories", "managed"].include?(association)
- Create a ticketCreate a ticket
- Exclude checks
Use !String#include?
instead of a regex match with literal-only pattern. Open
if col !~ /managed\./ && col !~ /virtual_custom/
- Create a ticketCreate a ticket
- Exclude checks
Avoid more than 3 levels of block nesting. Open
unless group_limit && group_counter >= group_limit # If not past the limit
html_rows += build_group_html_rows(save_val, col_order.length, break_label, group_text)
group_counter += 1
end
- 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
Vmdb::Plugins.map do |engine|
path = engine.root.join('product/views')
path if path.directory?
end.compact
- Create a ticketCreate a ticket
- Exclude checks
Avoid rescuing the Exception
class. Perhaps you meant to rescue StandardError
? Open
rescue Exception => err
MiqPolicy.logger.log_backtrace(err)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for rescue
blocks targeting the Exception class.
Example:
# bad
begin
do_something
rescue Exception
handle_exception
end
Example:
# good
begin
do_something
rescue ArgumentError
handle_exception
end
Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant. Open
next if %w[id created_on updated_on updated_by].include?(cname) || cname.ends_with?("_id")
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
tag.split("/").map { |x| x.split("|").second }.compact.join(" -> ")
- Create a ticketCreate a ticket
- Exclude checks
Use filter_map
instead. Open
miq_policy_contents.collect(&:miq_action).compact.uniq
- Create a ticketCreate a ticket
- Exclude checks
Call super
to initialize state of the parent class. Open
def initialize(values, requester, options = {})
initial_pass = values.blank?
initial_pass = true if options[:initial_pass] == true
instance_var_init(values, requester, options)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for the presence of constructors and lifecycle callbacks
without calls to super
.
This cop does not consider method_missing
(and respond_to_missing?
)
because in some cases it makes sense to overtake what is considered a
missing method. In other cases, the theoretical ideal handling could be
challenging or verbose for no actual gain.
Autocorrection is not supported because the position of super
cannot be
determined automatically.
Object
and BasicObject
are allowed by this cop because of their
stateless nature. However, sometimes you might want to allow other parent
classes from this cop, for example in the case of an abstract class that is
not meant to be called with super
. In those cases, you can use the
AllowedParentClasses
option to specify which classes should be allowed
in addition to Object
and BasicObject
.
Example:
# bad
class Employee < Person
def initialize(name, salary)
@salary = salary
end
end
# good
class Employee < Person
def initialize(name, salary)
super(name)
@salary = salary
end
end
# bad
Employee = Class.new(Person) do
def initialize(name, salary)
@salary = salary
end
end
# good
Employee = Class.new(Person) do
def initialize(name, salary)
super(name)
@salary = salary
end
end
# bad
class Parent
def self.inherited(base)
do_something
end
end
# good
class Parent
def self.inherited(base)
super
do_something
end
end
# good
class ClassWithNoParent
def initialize
do_something
end
end
Example: AllowedParentClasses: [MyAbstractClass]
# good
class MyConcreteClass < MyAbstractClass
def initialize
do_something
end
end