Checks for numeric comparisons that can be replaced
by a predicate method, such as receiver.length == 0,
receiver.length > 0, and receiver.length != 0,
receiver.length < 1 and receiver.size == 0 that can be
replaced by receiver.empty? and !receiver.empty?.
NOTE: File, Tempfile, and StringIO do not have empty?
so allow size == 0 and size.zero?.
Safety:
This cop is unsafe because it cannot be guaranteed that the receiver
has an empty? method that is defined in terms of length. If there
is a non-standard class that redefines length or empty?, the cop
may register a false positive.
Example:
# bad
[1, 2, 3].length == 0
0 == "foobar".length
array.length < 1
{a: 1, b: 2}.length != 0
string.length > 0
hash.size > 0
# good
[1, 2, 3].empty?
"foobar".empty?
array.empty?
!{a: 1, b: 2}.empty?
!string.empty?
!hash.empty?
Helps you transition from mutable string literals
to frozen string literals.
It will add the # frozen_string_literal: true magic comment to the top
of files to enable frozen string literals. Frozen string literals may be
default in future Ruby. The comment will be added below a shebang and
encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.
Note that the cop will accept files where the comment exists but is set
to false instead of true.
To require a blank line after this comment, please see
Layout/EmptyLineAfterMagicComment cop.
Safety:
This cop's autocorrection is unsafe since any strings mutations will
change from being accepted to raising FrozenError, as all strings
will become frozen by default, and will need to be manually refactored.
Example: EnforcedStyle: always (default)
# The `always` style will always add the frozen string literal comment
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# called on a string literal.
# bad
module Bar
# ...
end
# good
# frozen_string_literal: true
module Bar
# ...
end
# good
# frozen_string_literal: false
module Bar
# ...
end
Example: EnforcedStyle: never
# The `never` will enforce that the frozen string literal comment does
# not exist in a file.
# bad
# frozen_string_literal: true
module Baz
# ...
end
# good
module Baz
# ...
end
Example: EnforcedStyle: always_true
# The `always_true` style enforces that the frozen string literal
# comment is set to `true`. This is a stricter option than `always`
# and forces projects to use frozen string literals.
# bad
# frozen_string_literal: false
module Baz
# ...
end
# bad
module Baz
# ...
end
# good
# frozen_string_literal: true
module Bar
# ...
end
Checks for usage of comparison operators (==,
>, <) to test numbers as zero, positive, or negative.
These can be replaced by their respective predicate methods.
This cop can also be configured to do the reverse.
This cop can be customized allowed methods with AllowedMethods.
By default, there are no methods to allowed.
This cop disregards #nonzero? as its value is truthy or falsey,
but not true and false, and thus not always interchangeable with
!= 0.
This cop allows comparisons to global variables, since they are often
populated with objects which can be compared with integers, but are
not themselves Integer polymorphic.
Safety:
This cop is unsafe because it cannot be guaranteed that the receiver
defines the predicates or can be compared to a number, which may lead
to a false positive for non-standard classes.
Example: EnforcedStyle: predicate (default)
# bad
foo == 0
0 > foo
bar.baz > 0
# good
foo.zero?
foo.negative?
bar.baz.positive?
Example: EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
# good
foo == 0
0 > foo
bar.baz > 0
Example: AllowedMethods: [] (default) with EnforcedStyle: predicate
# bad
foo == 0
0 > foo
bar.baz > 0
Example: AllowedMethods: [==] with EnforcedStyle: predicate
# good
foo == 0
# bad
0 > foo
bar.baz > 0
Example: AllowedPatterns: [] (default) with EnforcedStyle: comparison
# bad
foo.zero?
foo.negative?
bar.baz.positive?
Example: AllowedPatterns: ['zero'] with EnforcedStyle: predicate
# good
# bad
foo.zero?
# bad
foo.negative?
bar.baz.positive?
Checks if uses of quotes match the configured preference.