Showing 66 of 66 total issues
Call super
to initialize state of the parent class. Open
def initialize(manager, target = nil, collector = nil)
@manager = manager
@target = target
@collector = collector
- 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
Empty block detected. Open
Benchmark.realtime_block(:connect) {}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for blocks without a body. Such empty blocks are typically an oversight or we should provide a comment be clearer what we're aiming for.
Empty lambdas and procs are ignored by default.
NOTE: For backwards compatibility, the configuration that allows/disallows
empty lambdas and procs is called AllowEmptyLambdas
, even though it also
applies to procs.
Example:
# bad
items.each { |item| }
# good
items.each { |item| puts item }
Example: AllowComments: true (default)
# good
items.each do |item|
# TODO: implement later (inner comment)
end
items.each { |item| } # TODO: implement later (inline comment)
Example: AllowComments: false
# bad
items.each do |item|
# TODO: implement later (inner comment)
end
items.each { |item| } # TODO: implement later (inline comment)
Example: AllowEmptyLambdas: true (default)
# good
allow(subject).to receive(:callable).and_return(-> {})
placeholder = lambda do
end
(callable || placeholder).call
proc { }
Proc.new { }
Example: AllowEmptyLambdas: false
# bad
allow(subject).to receive(:callable).and_return(-> {})
placeholder = lambda do
end
(callable || placeholder).call
proc { }
Proc.new { }
Use match?
instead of =~
when MatchData
is not used. Open
if ems_ref =~ /^https:/
- 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 filter_map
instead. Open
network_port.cloud_subnets.collect { |x| x.cloud_network.try(:ems_ref) }.compact.each { |ems_ref| add_target!(:cloud_networks, ems_ref) }
- Create a ticketCreate a ticket
- Exclude checks
Use match?
instead of =~
when MatchData
is not used. Open
return snapshot_info[:x_ms_snapshot] if snap_state =~ /succe/i
- 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 filter_map
instead. Open
all_stacks.collect(&:ems_ref).compact.each { |ems_ref| add_target!(:orchestration_stacks, ems_ref) }
- Create a ticketCreate a ticket
- Exclude checks