Showing 74 of 128 total issues
Remove explicit presence validation for user_id
. Open
validates :user_id, :name, presence: true
- Exclude checks
Remove explicit presence validation for user_id
. Open
validates :user_id, :name, :why, presence: true
- Exclude checks
Remove unnecessary existence check File.exist?
. Open
Dir.mkdir(DEFAULT_FILE_PATH) unless File.exist?(DEFAULT_FILE_PATH)
- Read upRead up
- Exclude checks
Checks for non-atomic file operation. And then replace it with a nearly equivalent and atomic method.
These can cause problems that are difficult to reproduce, especially in cases of frequent file operations in parallel, such as test runs with parallel_rspec.
For examples: creating a directory if there is none, has the following problems
An exception occurs when the directory didn't exist at the time of exist?
,
but someone else created it before mkdir
was executed.
Subsequent processes are executed without the directory that should be there
when the directory existed at the time of exist?
,
but someone else deleted it shortly afterwards.
Safety:
This cop is unsafe, because autocorrection change to atomic processing. The atomic processing of the replacement destination is not guaranteed to be strictly equivalent to that before the replacement.
Example:
# bad - race condition with another process may result in an error in `mkdir`
unless Dir.exist?(path)
FileUtils.mkdir(path)
end
# good - atomic and idempotent creation
FileUtils.mkdir_p(path)
# bad - race condition with another process may result in an error in `remove`
if File.exist?(path)
FileUtils.remove(path)
end
# good - atomic and idempotent removal
FileUtils.rm_f(path)
Remove explicit presence validation for user_id
. Open
validates :user_id, :name, :description, presence: true
- Exclude checks
Use find_each
instead of each
. Open
Strategy.all.each do |strategy|
- Read upRead up
- Exclude checks
This cop is used to identify usages of all.each
and
change them to use all.find_each
instead.
Example:
# bad
User.all.each
# good
User.all.find_each
Prefer string interpolation to string concatenation. Open
' | ' + t('layouts.title.reset_password')
- Read upRead up
- Exclude checks
Checks for places where string concatenation can be replaced with string interpolation.
The cop can autocorrect simple cases but will skip autocorrecting more complex cases where the resulting code would be harder to read. In those cases, it might be useful to extract statements to local variables or methods which you can then interpolate in a string.
NOTE: When concatenation between two strings is broken over multiple
lines, this cop does not register an offense; instead,
Style/LineEndConcatenation
will pick up the offense if enabled.
Two modes are supported:
1. aggressive
style checks and corrects all occurrences of +
where
either the left or right side of +
is a string literal.
2. conservative
style on the other hand, checks and corrects only if
left side (receiver of +
method call) is a string literal.
This is useful when the receiver is some expression that returns string like Pathname
instead of a string literal.
Safety:
This cop is unsafe in aggressive
mode, as it cannot be guaranteed that
the receiver is actually a string, which can result in a false positive.
Example: Mode: aggressive (default)
# bad
email_with_name = user.name + ' <' + user.email + '>'
Pathname.new('/') + 'test'
# good
email_with_name = "#{user.name} <#{user.email}>"
email_with_name = format('%s <%s>', user.name, user.email)
"#{Pathname.new('/')}test"
# accepted, line-end concatenation
name = 'First' +
'Last'
Example: Mode: conservative
# bad
'Hello' + user.name
# good
"Hello #{user.name}"
user.name + '!!'
Pathname.new('/') + 'test'
Remove explicit presence validation for group_id
/user_id
. Open
validates :group_id, :user_id, presence: true
- Exclude checks
Return false
instead of nil
in predicate methods. Open
return if omniauth_avatar.nil?
- Read upRead up
- Exclude checks
Checks if return
or return nil
is used in predicate method definitions.
Safety:
Autocorrection is marked as unsafe because the change of the return value
from nil
to false
could potentially lead to incompatibility issues.
Example:
# bad
def foo?
return if condition
do_something?
end
# bad
def foo?
return nil if condition
do_something?
end
# good
def foo?
return false if condition
do_something?
end
Example: AllowedMethods: ['foo?']
# good
def foo?
return if condition
do_something?
end
Example: AllowedPatterns: [/foo/]
# good
def foo?
return if condition
do_something?
end
Prefer string interpolation to string concatenation. Open
' | ' + t('account.sign_up')
- Read upRead up
- Exclude checks
Checks for places where string concatenation can be replaced with string interpolation.
The cop can autocorrect simple cases but will skip autocorrecting more complex cases where the resulting code would be harder to read. In those cases, it might be useful to extract statements to local variables or methods which you can then interpolate in a string.
NOTE: When concatenation between two strings is broken over multiple
lines, this cop does not register an offense; instead,
Style/LineEndConcatenation
will pick up the offense if enabled.
Two modes are supported:
1. aggressive
style checks and corrects all occurrences of +
where
either the left or right side of +
is a string literal.
2. conservative
style on the other hand, checks and corrects only if
left side (receiver of +
method call) is a string literal.
This is useful when the receiver is some expression that returns string like Pathname
instead of a string literal.
Safety:
This cop is unsafe in aggressive
mode, as it cannot be guaranteed that
the receiver is actually a string, which can result in a false positive.
Example: Mode: aggressive (default)
# bad
email_with_name = user.name + ' <' + user.email + '>'
Pathname.new('/') + 'test'
# good
email_with_name = "#{user.name} <#{user.email}>"
email_with_name = format('%s <%s>', user.name, user.email)
"#{Pathname.new('/')}test"
# accepted, line-end concatenation
name = 'First' +
'Last'
Example: Mode: conservative
# bad
'Hello' + user.name
# good
"Hello #{user.name}"
user.name + '!!'
Pathname.new('/') + 'test'
Rails.public_path
is a Pathname
so you can just append #read
. Open
File.read(Rails.public_path.join(webpack_folder, path))
- Exclude checks
Prefer keyword arguments for arguments with a boolean default value; use edit: false
instead of edit = false
. Open
def moment_form_inputs(edit = false)
- Read upRead up
- Exclude checks
Checks for places where keyword arguments can be used instead of
boolean arguments when defining methods. respond_to_missing?
method is allowed by default.
These are customizable with AllowedMethods
option.
Safety:
This cop is unsafe because changing a method signature will implicitly change behavior.
Example:
# bad
def some_method(bar = false)
puts bar
end
# bad - common hack before keyword args were introduced
def some_method(options = {})
bar = options.fetch(:bar, false)
puts bar
end
# good
def some_method(bar: false)
puts bar
end
Example: AllowedMethods: ['some_method']
# good
def some_method(bar = false)
puts bar
end
Remove explicit presence validation for reporter_id
. Open
validates :reporter_id, presence: true
- Exclude checks
Move locale texts to the locale files in the config/locales
directory. Open
message: 'There is already a request with this request_id.'
- Exclude checks
Prefer grep
to select
with a regexp match. Open
resource['tags'].select do |tag|
@moment_keywords.match?(tag)
end
- Read upRead up
- Exclude checks
Looks for places where an subset of an Enumerable (array,
range, set, etc.; see note below) is calculated based on a Regexp
match, and suggests grep
or grep_v
instead.
NOTE: Hashes do not behave as you may expect with grep
, which
means that hash.grep
is not equivalent to hash.select
. Although
RuboCop is limited by static analysis, this cop attempts to avoid
registering an offense when the receiver is a hash (hash literal,
Hash.new
, Hash#[]
, or to_h
/to_hash
).
NOTE: grep
and grep_v
were optimized when used without a block
in Ruby 3.0, but may be slower in previous versions.
See https://bugs.ruby-lang.org/issues/17030
Safety:
Autocorrection is marked as unsafe because MatchData
will
not be created by grep
, but may have previously been relied
upon after the match?
or =~
call.
Additionally, the cop cannot guarantee that the receiver of
select
or reject
is actually an array by static analysis,
so the correction may not be actually equivalent.
Example:
# bad (select or find_all)
array.select { |x| x.match? /regexp/ }
array.select { |x| /regexp/.match?(x) }
array.select { |x| x =~ /regexp/ }
array.select { |x| /regexp/ =~ x }
# bad (reject)
array.reject { |x| x.match? /regexp/ }
array.reject { |x| /regexp/.match?(x) }
array.reject { |x| x =~ /regexp/ }
array.reject { |x| /regexp/ =~ x }
# good
array.grep(regexp)
array.grep_v(regexp)