Showing 211 of 211 total issues
Assignment Branch Condition size for import is too high. [<15, 37, 12> 41.69/17] Open
def import(save: true, status_tracker: nil)
self.class.before.call(@context, options) if self.class.before.is_a?(Proc)
at = 0
errors_lines = []
success_count = 0
- Read upRead up
- Exclude checks
Checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric and https://en.wikipedia.org/wiki/ABC_Software_Metric.
Interpreting ABC size:
-
<= 17
satisfactory -
18..30
unsatisfactory -
>
30 dangerous
You can have repeated "attributes" calls count as a single "branch".
For this purpose, attributes are any method with no argument; no attempt
is meant to distinguish actual attr_reader
from other methods.
Example: CountRepeatedAttributes: false (default is true)
# `model` and `current_user`, referenced 3 times each,
# are each counted as only 1 branch each if
# `CountRepeatedAttributes` is set to 'false'
def search
@posts = model.active.visible_by(current_user)
.search(params[:q])
@posts = model.some_process(@posts, current_user)
@posts = model.another_process(@posts, current_user)
render 'pages/search/page'
end
This cop also takes into account AllowedMethods
(defaults to []
)
And AllowedPatterns
(defaults to []
)
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. Open
@action = record.persisted? ? 'update' : 'create'
- Read upRead up
- Exclude checks
Checks if uses of quotes match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
"No special symbols"
"No string interpolation"
"Just text"
# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"
Example: EnforcedStyle: double_quotes
# bad
'Just some text'
'No special chars or interpolation'
# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"
Use 2 (not 0) spaces for indentation. Open
def export
- Read upRead up
- Exclude checks
Checks for indentation that doesn't use the specified number of spaces.
See also the IndentationConsistency cop which is the companion to this one.
Example:
# bad
class A
def test
puts 'hello'
end
end
# good
class A
def test
puts 'hello'
end
end
Example: AllowedPatterns: ['^\s*module']
# bad
module A
class B
def test
puts 'hello'
end
end
end
# good
module A
class B
def test
puts 'hello'
end
end
end
Shadowing outer local variable - index
. Open
@data.each_with_index do |record, index|
- Read upRead up
- Exclude checks
Checks for the use of local variable names from an outer scope
in block arguments or block-local variables. This mirrors the warning
given by ruby -cw
prior to Ruby 2.6:
"shadowing outer local variable - foo".
NOTE: Shadowing of variables in block passed to Ractor.new
is allowed
because Ractor
should not access outer variables.
eg. following style is encouraged:
```ruby
worker_id, pipe = env
Ractor.new(worker_id, pipe) do |worker_id, pipe|
end
```
Example:
# bad
def some_method
foo = 1
2.times do |foo| # shadowing outer `foo`
do_something(foo)
end
end
Example:
# good
def some_method
foo = 1
2.times do |bar|
do_something(bar)
end
end
Group together all attr_accessor
attributes. Open
attr_accessor :context
- Read upRead up
- Exclude checks
Checks for grouping of accessors in class
and module
bodies.
By default it enforces accessors to be placed in grouped declarations,
but it can be configured to enforce separating them in multiple declarations.
NOTE: If there is a method call before the accessor method it is always allowed as it might be intended like Sorbet.
Example: EnforcedStyle: grouped (default)
# bad
class Foo
attr_reader :bar
attr_reader :bax
attr_reader :baz
end
# good
class Foo
attr_reader :bar, :bax, :baz
end
# good
class Foo
# may be intended comment for bar.
attr_reader :bar
sig { returns(String) }
attr_reader :bax
may_be_intended_annotation :baz
attr_reader :baz
end
Example: EnforcedStyle: separated
# bad
class Foo
attr_reader :bar, :baz
end
# good
class Foo
attr_reader :bar
attr_reader :baz
end
Freeze mutable objects assigned to constants. Open
DEFAULT_STYLES = {
date_format: {
format_code: 'dd-mm-yyyy'
},
time_format: {
- Read upRead up
- Exclude checks
Checks whether some constant value isn't a mutable literal (e.g. array or hash).
Strict mode can be used to freeze all constants, rather than just literals. Strict mode is considered an experimental feature. It has not been updated with an exhaustive list of all methods that will produce frozen objects so there is a decent chance of getting some false positives. Luckily, there is no harm in freezing an already frozen object.
From Ruby 3.0, this cop honours the magic comment 'shareableconstantvalue'. When this magic comment is set to any acceptable value other than none, it will suppress the offenses raised by this cop. It enforces frozen state.
NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
NOTE: From Ruby 3.0, interpolated strings are not frozen when
# frozen-string-literal: true
is used, so this cop enforces explicit
freezing for such strings.
NOTE: From Ruby 3.0, this cop allows explicit freezing of constants when
the shareable_constant_value
directive is used.
Safety:
This cop's autocorrection is unsafe since any mutations on objects that
are made frozen will change from being accepted to raising FrozenError
,
and will need to be manually refactored.
Example: EnforcedStyle: literals (default)
# bad
CONST = [1, 2, 3]
# good
CONST = [1, 2, 3].freeze
# good
CONST = <<~TESTING.freeze
This is a heredoc
TESTING
# good
CONST = Something.new
Example: EnforcedStyle: strict
# bad
CONST = Something.new
# bad
CONST = Struct.new do
def foo
puts 1
end
end
# good
CONST = Something.new.freeze
# good
CONST = Struct.new do
def foo
puts 1
end
end.freeze
Example:
# Magic comment - shareable_constant_value: literal
# bad
CONST = [1, 2, 3]
# good
# shareable_constant_value: literal
CONST = [1, 2, 3]
Use columns_with_children.size.positive?
instead of columns_with_children.size > 0
. Open
return count unless columns_with_children && columns_with_children.size > 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?
Prefer double-quoted strings inside interpolations. Open
"#{lock.include?(:col) ? '$' : ''}#{column_name(col)}#{lock.include?(:row) ? '$' : ''}#{row}"
- Read upRead up
- Exclude checks
Checks that quotes inside string, symbol, and regexp interpolations match the configured preference.
Example: EnforcedStyle: single_quotes (default)
# bad
string = "Tests #{success ? "PASS" : "FAIL"}"
symbol = :"Tests #{success ? "PASS" : "FAIL"}"
heredoc = <<~TEXT
Tests #{success ? "PASS" : "FAIL"}
TEXT
regexp = /Tests #{success ? "PASS" : "FAIL"}/
# good
string = "Tests #{success ? 'PASS' : 'FAIL'}"
symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
heredoc = <<~TEXT
Tests #{success ? 'PASS' : 'FAIL'}
TEXT
regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
Example: EnforcedStyle: double_quotes
# bad
string = "Tests #{success ? 'PASS' : 'FAIL'}"
symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
heredoc = <<~TEXT
Tests #{success ? 'PASS' : 'FAIL'}
TEXT
regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
# good
string = "Tests #{success ? "PASS" : "FAIL"}"
symbol = :"Tests #{success ? "PASS" : "FAIL"}"
heredoc = <<~TEXT
Tests #{success ? "PASS" : "FAIL"}
TEXT
regexp = /Tests #{success ? "PASS" : "FAIL"}/
Do not use do
with multi-line while
. Open
while index >= 26 do
- Read upRead up
- Exclude checks
Checks for uses of do
in multi-line while/until
statements.
Example:
# bad
while x.any? do
do_something(x.pop)
end
# good
while x.any?
do_something(x.pop)
end
Example:
# bad
until x.empty? do
do_something(x.pop)
end
# good
until x.empty?
do_something(x.pop)
end
Redundant safe navigation detected, use .
instead. Open
elsif header_found&.is_a?(String)
- Read upRead up
- Exclude checks
Checks for redundant safe navigation calls.
Use cases where a constant, named in camel case for classes and modules is nil
are rare,
and an offense is not detected when the receiver is a constant. The detection also applies
to literal receivers, except for nil
.
For all receivers, the instance_of?
, kind_of?
, is_a?
, eql?
, respond_to?
,
and equal?
methods are checked by default.
These are customizable with AllowedMethods
option.
The AllowedMethods
option specifies nil-safe methods,
in other words, it is a method that is allowed to skip safe navigation.
Note that the AllowedMethod
option is not an option that specifies methods
for which to suppress (allow) this cop's check.
In the example below, the safe navigation operator (&.
) is unnecessary
because NilClass
has methods like respond_to?
and is_a?
.
Safety:
This cop is unsafe, because autocorrection can change the return type of
the expression. An offending expression that previously could return nil
will be autocorrected to never return nil
.
Example:
# bad
CamelCaseConst&.do_something
# bad
do_something if attrs&.respond_to?(:[])
# good
do_something if attrs.respond_to?(:[])
# bad
while node&.is_a?(BeginNode)
node = node.parent
end
# good
CamelCaseConst.do_something
# good
while node.is_a?(BeginNode)
node = node.parent
end
# good - without `&.` this will always return `true`
foo&.respond_to?(:to_a)
# bad - for `nil`s conversion methods return default values for the type
foo&.to_h || {}
foo&.to_h { |k, v| [k, v] } || {}
foo&.to_a || []
foo&.to_i || 0
foo&.to_f || 0.0
foo&.to_s || ''
# good
foo.to_h
foo.to_h { |k, v| [k, v] }
foo.to_a
foo.to_i
foo.to_f
foo.to_s
Example: AllowedMethods: [nilsafemethod]
# bad
do_something if attrs&.nil_safe_method(:[])
# good
do_something if attrs.nil_safe_method(:[])
do_something if attrs&.not_nil_safe_method(:[])
Use the return of the conditional for variable assignment and comparison. Open
if save
@success = record.save
else
@success = record.valid?
end
- Exclude checks