Use actual_consumption.positive?
instead of actual_consumption > 0
. Open
actual_consumption > 0 ? actual_consumption : 0.0
- Read upRead up
- Exclude checks
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?
Missing space after #
. Open
#TODO Make this a scope
- Read upRead up
- Exclude checks
Checks whether comments have a leading space after the
#
denoting the start of the comment. The leading space is not
required for some RDoc special syntax, like #++
, #--
,
#:nodoc
, =begin
- and =end
comments, "shebang" directives,
or rackup options.
Example:
# bad
#Some comment
# good
# Some comment
Example: AllowDoxygenCommentStyle: false (default)
# bad
#**
# Some comment
# Another line of comment
#*
Example: AllowDoxygenCommentStyle: true
# good
#**
# Some comment
# Another line of comment
#*
Example: AllowGemfileRubyComment: false (default)
# bad
#ruby=2.7.0
#ruby-gemset=myproject
Example: AllowGemfileRubyComment: true
# good
#ruby=2.7.0
#ruby-gemset=myproject
Annotation keywords like TODO
should be all upper case, followed by a colon, and a space, then a note describing the problem. Open
#TODO Make this a scope
- Read upRead up
- Exclude checks
Checks that comment annotation keywords are written according to guidelines.
Annotation keywords can be specified by overriding the cop's Keywords
configuration. Keywords are allowed to be single words or phrases.
NOTE: With a multiline comment block (where each line is only a
comment), only the first line will be able to register an offense, even
if an annotation keyword starts another line. This is done to prevent
incorrect registering of keywords (eg. review
) inside a paragraph as an
annotation.
Example: RequireColon: true (default)
# bad
# TODO make better
# good
# TODO: make better
# bad
# TODO:make better
# good
# TODO: make better
# bad
# fixme: does not work
# good
# FIXME: does not work
# bad
# Optimize does not work
# good
# OPTIMIZE: does not work
Example: RequireColon: false
# bad
# TODO: make better
# good
# TODO make better
# bad
# fixme does not work
# good
# FIXME does not work
# bad
# Optimize does not work
# good
# OPTIMIZE does not work
Missing top-level documentation comment for class Estimation
. Open
class Estimation < ActiveRecord::Base
- Read upRead up
- Exclude checks
Checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, constant definitions or constant visibility declarations.
The documentation requirement is annulled if the class or module has
a #:nodoc:
comment next to it. Likewise, #:nodoc: all
does the
same for all its children.
Example:
# bad
class Person
# ...
end
module Math
end
# good
# Description/Explanation of Person class
class Person
# ...
end
# allowed
# Class without body
class Person
end
# Namespace - A namespace can be a class or a module
# Containing a class
module Namespace
# Description/Explanation of Person class
class Person
# ...
end
end
# Containing constant visibility declaration
module Namespace
class Private
end
private_constant :Private
end
# Containing constant definition
module Namespace
Public = Class.new
end
# Macro calls
module Namespace
extend Foo
end
Example: AllowedConstants: ['ClassMethods']
# good
module A
module ClassMethods
# ...
end
end
Missing frozen string literal comment. Open
# == Schema Information
- Read upRead up
- Exclude checks
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
Surrounding space missing for operator /
. Open
bufferable_sum/Math.sqrt(bufferable_count)
- Read upRead up
- Exclude checks
Checks that operators have space around them, except for ** which should or shouldn't have surrounding space depending on configuration. It allows vertical alignment consisting of one or more whitespace around operators.
This cop has AllowForAlignment
option. When true
, allows most
uses of extra spacing if the intent is to align with an operator on
the previous or next line, not counting empty lines or comment lines.
Example:
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
Example: AllowForAlignment: true (default)
# good
{
1 => 2,
11 => 3
}
Example: AllowForAlignment: false
# bad
{
1 => 2,
11 => 3
}
Example: EnforcedStyleForExponentOperator: no_space (default)
# bad
a ** b
# good
a**b
Example: EnforcedStyleForExponentOperator: space
# bad
a**b
# good
a ** b
Example: EnforcedStyleForRationalLiterals: no_space (default)
# bad
1 / 48r
# good
1/48r
Example: EnforcedStyleForRationalLiterals: space
# bad
1/48r
# good
1 / 48r
Annotation keywords like TODO
should be all upper case, followed by a colon, and a space, then a note describing the problem. Open
#TODO Make this a scope
- Read upRead up
- Exclude checks
Checks that comment annotation keywords are written according to guidelines.
Annotation keywords can be specified by overriding the cop's Keywords
configuration. Keywords are allowed to be single words or phrases.
NOTE: With a multiline comment block (where each line is only a
comment), only the first line will be able to register an offense, even
if an annotation keyword starts another line. This is done to prevent
incorrect registering of keywords (eg. review
) inside a paragraph as an
annotation.
Example: RequireColon: true (default)
# bad
# TODO make better
# good
# TODO: make better
# bad
# TODO:make better
# good
# TODO: make better
# bad
# fixme: does not work
# good
# FIXME: does not work
# bad
# Optimize does not work
# good
# OPTIMIZE: does not work
Example: RequireColon: false
# bad
# TODO: make better
# good
# TODO make better
# bad
# fixme does not work
# good
# FIXME does not work
# bad
# Optimize does not work
# good
# OPTIMIZE does not work
Use count.zero?
instead of count == 0
. Open
count == 0 ? 1 : count
- Read upRead up
- Exclude checks
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?
Missing space after #
. Open
#TODO Make this a scope
- Read upRead up
- Exclude checks
Checks whether comments have a leading space after the
#
denoting the start of the comment. The leading space is not
required for some RDoc special syntax, like #++
, #--
,
#:nodoc
, =begin
- and =end
comments, "shebang" directives,
or rackup options.
Example:
# bad
#Some comment
# good
# Some comment
Example: AllowDoxygenCommentStyle: false (default)
# bad
#**
# Some comment
# Another line of comment
#*
Example: AllowDoxygenCommentStyle: true
# good
#**
# Some comment
# Another line of comment
#*
Example: AllowGemfileRubyComment: false (default)
# bad
#ruby=2.7.0
#ruby-gemset=myproject
Example: AllowGemfileRubyComment: true
# good
#ruby=2.7.0
#ruby-gemset=myproject
Keep a blank line before and after private
. Open
private
- Read upRead up
- Exclude checks
Access modifiers should be surrounded by blank lines.
Example: EnforcedStyle: around (default)
# bad
class Foo
def bar; end
private
def baz; end
end
# good
class Foo
def bar; end
private
def baz; end
end
Example: EnforcedStyle: only_before
# bad
class Foo
def bar; end
private
def baz; end
end
# good
class Foo
def bar; end
private
def baz; end
end
Surrounding space missing for operator /
. Open
progress = completed_items.sum('value * quantity')/sum
- Read upRead up
- Exclude checks
Checks that operators have space around them, except for ** which should or shouldn't have surrounding space depending on configuration. It allows vertical alignment consisting of one or more whitespace around operators.
This cop has AllowForAlignment
option. When true
, allows most
uses of extra spacing if the intent is to align with an operator on
the previous or next line, not counting empty lines or comment lines.
Example:
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
Example: AllowForAlignment: true (default)
# good
{
1 => 2,
11 => 3
}
Example: AllowForAlignment: false
# bad
{
1 => 2,
11 => 3
}
Example: EnforcedStyleForExponentOperator: no_space (default)
# bad
a ** b
# good
a**b
Example: EnforcedStyleForExponentOperator: space
# bad
a**b
# good
a ** b
Example: EnforcedStyleForRationalLiterals: no_space (default)
# bad
1 / 48r
# good
1/48r
Example: EnforcedStyleForRationalLiterals: space
# bad
1/48r
# good
1 / 48r
Surrounding space missing for operator /
. Open
actual_consumption = (completed_items.sum(:actual_value) - completed_items.sum('value * quantity'))/buffer
- Read upRead up
- Exclude checks
Checks that operators have space around them, except for ** which should or shouldn't have surrounding space depending on configuration. It allows vertical alignment consisting of one or more whitespace around operators.
This cop has AllowForAlignment
option. When true
, allows most
uses of extra spacing if the intent is to align with an operator on
the previous or next line, not counting empty lines or comment lines.
Example:
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
Example: AllowForAlignment: true (default)
# good
{
1 => 2,
11 => 3
}
Example: AllowForAlignment: false
# bad
{
1 => 2,
11 => 3
}
Example: EnforcedStyleForExponentOperator: no_space (default)
# bad
a ** b
# good
a**b
Example: EnforcedStyleForExponentOperator: space
# bad
a**b
# good
a ** b
Example: EnforcedStyleForRationalLiterals: no_space (default)
# bad
1 / 48r
# good
1/48r
Example: EnforcedStyleForRationalLiterals: space
# bad
1/48r
# good
1 / 48r
Use progress.positive?
instead of progress > 0
. Open
progress > 0 ? progress : 0.0
- Read upRead up
- Exclude checks
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?
Surrounding space missing for operator /
. Open
health = buffer_consumption/project_progress
- Read upRead up
- Exclude checks
Checks that operators have space around them, except for ** which should or shouldn't have surrounding space depending on configuration. It allows vertical alignment consisting of one or more whitespace around operators.
This cop has AllowForAlignment
option. When true
, allows most
uses of extra spacing if the intent is to align with an operator on
the previous or next line, not counting empty lines or comment lines.
Example:
# bad
total = 3*4
"apple"+"juice"
my_number = 38/4
# good
total = 3 * 4
"apple" + "juice"
my_number = 38 / 4
Example: AllowForAlignment: true (default)
# good
{
1 => 2,
11 => 3
}
Example: AllowForAlignment: false
# bad
{
1 => 2,
11 => 3
}
Example: EnforcedStyleForExponentOperator: no_space (default)
# bad
a ** b
# good
a**b
Example: EnforcedStyleForExponentOperator: space
# bad
a**b
# good
a ** b
Example: EnforcedStyleForRationalLiterals: no_space (default)
# bad
1 / 48r
# good
1/48r
Example: EnforcedStyleForRationalLiterals: space
# bad
1/48r
# good
1 / 48r