Showing 1,005 of 1,005 total issues
Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem config
should appear before connection_pool
. Open
gem "config", "~>2.2", ">=2.2.3", :require => false
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Gems should be alphabetically sorted within groups.
Example:
# bad
gem 'rubocop'
gem 'rspec'
# good
gem 'rspec'
gem 'rubocop'
# good
gem 'rubocop'
gem 'rspec'
# good only if TreatCommentsAsGroupSeparators is true
# For code quality
gem 'rubocop'
# For tests
gem 'rspec'
Parenthesize the param Module.new {
def resolve_class(klass_name)
(class_loader.class != Psych::ClassLoader::Restricted && klass_name && klass_name.safe_constantize) || super
end
}
to make sure that the block will be associated with the Module.new
method call. Open
Psych::Visitors::ToRuby.prepend Module.new {
def resolve_class(klass_name)
(class_loader.class != Psych::ClassLoader::Restricted && klass_name && klass_name.safe_constantize) || super
end
}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for ambiguous block association with method when param passed without parentheses.
Example:
# bad
some_method a { |val| puts val }
Example:
# good
# With parentheses, there's no ambiguity.
some_method(a) { |val| puts val }
# good
# Operator methods require no disambiguation
foo == bar { |b| b.baz }
# good
# Lambda arguments require no disambiguation
foo = ->(bar) { bar.baz }
Avoid when
branches without a body. Open
when "raw" # return without formatting
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for the presence of when
branches without a body.
Example:
# bad
case foo
when bar then 1
when baz then # nothing
end
Example:
# good
case foo
when bar then 1
when baz then 2
end
Use match?
instead of =~
when MatchData
is not used. Open
:no_db if error.message =~ /Connection refused/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 #key?
instead of #keys.include?
. Open
return false if exp[operator].keys.include?("tag")
- Create a ticketCreate a ticket
- Exclude checks
Avoid rescuing the Exception
class. Perhaps you meant to rescue StandardError
? Open
rescue Exception => err
_log.error("'#{err.message}'")
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop 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 rescuing the Exception
class. Perhaps you meant to rescue StandardError
? Open
rescue Exception => err
MiqWorker::Runner.safe_log(worker, "An unhandled error has occurred: #{err}\n#{err.backtrace.join("\n")}", :error)
STDERR.puts("ERROR: An unhandled error has occurred: #{err}. See log for details.") rescue nil
exit 1
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop 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 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
This cop 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 match?
instead of =~
when MatchData
is not used. Open
if ipaddr =~ Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex).freeze
- 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 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.
Use #key?
instead of #keys.include?
. Open
if exp[operator].keys.include?("field") && exp[operator]["field"].split(".").length == 1
- Create a ticketCreate a ticket
- Exclude checks
Use #key?
instead of #keys.include?
. Open
return false if exp[operator].keys.include?("regkey")
- Create a ticketCreate a ticket
- Exclude checks
Unused block argument - type
. If it's necessary, use _
or _type
as an argument name to indicate that it won't be used. Open
VC_ACCESSORS.each do |accessor, type|
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
# good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Example: IgnoreEmptyBlocks: true (default)
# good
do_something { |unused| }
Example: IgnoreEmptyBlocks: false
# bad
do_something { |unused| }
Example: AllowUnusedKeywordArguments: false (default)
# bad
do_something do |unused: 42|
foo
end
Example: AllowUnusedKeywordArguments: true
# good
do_something do |unused: 42|
foo
end
Use match?
instead of =~
when MatchData
is not used. Open
raise unless e.message =~ /already exists/
- 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
Parenthesize the param detail
.except(:name)
.map { |k, v| " #{k}: #{v}" }
to make sure that the block will be associated with the detail
.except(:name)
.map
method call. Open
puts detail
.except(:name)
.map { |k, v| " #{k}: #{v}" }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for ambiguous block association with method when param passed without parentheses.
Example:
# bad
some_method a { |val| puts val }
Example:
# good
# With parentheses, there's no ambiguity.
some_method(a) { |val| puts val }
# good
# Operator methods require no disambiguation
foo == bar { |b| b.baz }
# good
# Lambda arguments require no disambiguation
foo = ->(bar) { bar.baz }
Prefer using YAML.safe_load
over YAML.load
. Open
dialogs = YAML.load(import_file_upload.uploaded_content)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for the use of YAML class methods which have potential security issues leading to remote code execution when loading from an untrusted source.
Example:
# bad
YAML.load("--- foo")
# good
YAML.safe_load("--- foo")
YAML.dump("foo")
Avoid rescuing the Exception
class. Perhaps you meant to rescue StandardError
? Open
rescue Exception => err
_log.error("Binding to LDAP: Host: [#{@ldap.host}], User: [#{username}], '#{err.message}'")
return false
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop 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
Use ==
if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition. Open
if pid = MiqServer.running?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for assignments in the conditions of if/while/until.
AllowSafeAssignment
option for safe assignment.
By safe assignment we mean putting parentheses around
an assignment to indicate "I know I'm using an assignment
as a condition. It's not a mistake."
Example:
# bad
if some_var = true
do_something
end
# good
if some_var == true
do_something
end
Example: AllowSafeAssignment: true (default)
# good
if (some_var = true)
do_something
end
Example: AllowSafeAssignment: false
# bad
if (some_var = true)
do_something
end
Ambiguous splat operator. Parenthesize the method arguments if it's surely a splat operator, or add a whitespace to the right of the *
if it should be a multiplication. Open
module_eval { attr_reader *args }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.
Example:
# bad
# The `*` is interpreted as a splat operator but it could possibly be
# a `*` method invocation (i.e. `do_something.*(some_array)`).
do_something *some_array
Example:
# good
# With parentheses, there's no ambiguity.
do_something(*some_array)
Prefer using YAML.safe_load
over YAML.load
. Open
obj = YAML.load(data)
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for the use of YAML class methods which have potential security issues leading to remote code execution when loading from an untrusted source.
Example:
# bad
YAML.load("--- foo")
# good
YAML.safe_load("--- foo")
YAML.dump("foo")