Showing 1,637 of 1,651 total issues
Use safe navigation (&.
) instead of checking if an object exists before calling the method. Open
@ftp.close unless @ftp.nil?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop transforms usages of a method call safeguarded by a non nil
check for the variable whose method is being called to
safe navigation (&.
).
Configuration option: ConvertCodeThatCanStartToReturnNil
The default for this is false
. When configured to true
, this will
check for code in the format !foo.nil? && foo.bar
. As it is written,
the return of this code is limited to false
and whatever the return
of the method is. If this is converted to safe navigation,
foo&.bar
can start returning nil
as well as what the method
returns.
Example:
# bad
foo.bar if foo
foo.bar(param1, param2) if foo
foo.bar { |e| e.something } if foo
foo.bar(param) { |e| e.something } if foo
foo.bar if !foo.nil?
foo.bar unless !foo
foo.bar unless foo.nil?
foo && foo.bar
foo && foo.bar(param1, param2)
foo && foo.bar { |e| e.something }
foo && foo.bar(param) { |e| e.something }
# good
foo&.bar
foo&.bar(param1, param2)
foo&.bar { |e| e.something }
foo&.bar(param) { |e| e.something }
foo.nil? || foo.bar
!foo || foo.bar
# Methods that `nil` will `respond_to?` should not be converted to
# use safe navigation
foo.to_i if foo
Missing magic comment # frozen_string_literal: true
. Open
# @package MiGA
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop is designed to help upgrade to Ruby 3.0. It will add the
comment # frozen_string_literal: true
to the top of files to
enable frozen string literals. Frozen string literals may be default
in Ruby 3.0. The comment will be added below a shebang and encoding
comment. The frozen string literal comment is only valid in Ruby 2.3+.
Example: EnforcedStyle: when_needed (default)
# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
# ...
end
# good
# frozen_string_literal: true
module Foo
# ...
end
Example: EnforcedStyle: always
# 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
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
Put empty method definitions on a single line. Open
def complete
end
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for the formatting of empty method definitions.
By default it enforces empty method definitions to go on a single
line (compact style), but it can be configured to enforce the end
to go on its own line (expanded style).
Note: A method definition is not considered empty if it contains comments.
Example: EnforcedStyle: compact (default)
# bad
def foo(bar)
end
def self.foo(bar)
end
# good
def foo(bar); end
def foo(bar)
# baz
end
def self.foo(bar); end
Example: EnforcedStyle: expanded
# bad
def foo(bar); end
def self.foo(bar); end
# good
def foo(bar)
end
def self.foo(bar)
end
Use ||
instead of or
. Open
next unless cli[:force] or d.done_preprocessing?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for uses of and
and or
, and suggests using &&
and
|| instead
. It can be configured to check only in conditions, or in
all contexts.
Example: EnforcedStyle: always (default)
# bad
foo.save and return
# bad
if foo and bar
end
# good
foo.save && return
# good
if foo && bar
end
Example: EnforcedStyle: conditionals
# bad
if foo and bar
end
# good
foo.save && return
# good
foo.save and return
# good
if foo && bar
end
Unnecessary splat expansion. Open
when *%w[project file verbose help debug]
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for unneeded usages of splat expansion
Example:
# bad
a = *[1, 2, 3]
a = *'a'
a = *1
begin
foo
rescue *[StandardError, ApplicationError]
bar
end
case foo
when *[1, 2, 3]
bar
else
baz
end
Example:
# good
c = [1, 2, 3]
a = *c
a, b = *c
a, *b = *c
a = *1..10
a = ['a']
begin
foo
rescue StandardError, ApplicationError
bar
end
case foo
when *[1, 2, 3]
bar
else
baz
end
Shadowing outer local variable - k
. Open
stats.map { |s| keys.map { |k| s[k].is_a?(Array) ? s[k].first : s[k] } }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop looks for use of the same name as outer local variables
for block arguments or block local variables.
This is a mimic of the warning
"shadowing outer local variable - foo" from ruby -cw
.
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
Ternary operators must not be nested. Prefer if
or else
constructs instead. Open
stats.map { |s| keys.map { |k| s[k].is_a?(Array) ? s[k].first : s[k] } }
- Create a ticketCreate a ticket
- Exclude checks
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 cli[:result] && r = cli.load_result
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for assignments in the conditions of if/while/until.
Example:
# bad
if some_var = true
do_something
end
Example:
# good
if some_var == true
do_something
end
Missing magic comment # frozen_string_literal: true
. Open
# @package MiGA
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop is designed to help upgrade to Ruby 3.0. It will add the
comment # frozen_string_literal: true
to the top of files to
enable frozen string literals. Frozen string literals may be default
in Ruby 3.0. The comment will be added below a shebang and encoding
comment. The frozen string literal comment is only valid in Ruby 2.3+.
Example: EnforcedStyle: when_needed (default)
# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
# ...
end
# good
# frozen_string_literal: true
module Foo
# ...
end
Example: EnforcedStyle: always
# 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
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
Do not use semicolons to terminate expressions. Open
) { |v| puts MiGA::MiGA.LONG_VERSION; exit }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
Example:
# bad
foo = 1; bar = 2;
baz = 3;
# good
foo = 1
bar = 2
baz = 3
Do not use semicolons to terminate expressions. Open
) { |v| puts MiGA::MiGA.CITATION; exit }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
Example:
# bad
foo = 1; bar = 2;
baz = 3;
# good
foo = 1
bar = 2
baz = 3
Avoid using {...}
for multi-line blocks. Open
stats.map { |s|
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Check for uses of braces or do/end around single line or multi-line blocks.
Example: EnforcedStyle: linecountbased (default)
# bad - single line block
items.each do |item| item / 5 end
# good - single line block
items.each { |item| item / 5 }
# bad - multi-line block
things.map { |thing|
something = thing.some_method
process(something)
}
# good - multi-line block
things.map do |thing|
something = thing.some_method
process(something)
end
Example: EnforcedStyle: semantic
# Prefer `do...end` over `{...}` for procedural blocks.
# return value is used/assigned
# bad
foo = map do |x|
x
end
puts (map do |x|
x
end)
# return value is not used out of scope
# good
map do |x|
x
end
# Prefer `{...}` over `do...end` for functional blocks.
# return value is not used out of scope
# bad
each { |x|
x
}
# return value is used/assigned
# good
foo = map { |x|
x
}
map { |x|
x
}.inspect
Example: EnforcedStyle: bracesforchaining
# bad
words.each do |word|
word.flip.flop
end.join("-")
# good
words.each { |word|
word.flip.flop
}.join("-")
Space found before semicolon. Open
) { |v| cli[:activate] = false ; cli[:reason] = v }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for semicolon (;) preceded by space.
Example:
# bad
x = 1 ; y = 2
# good
x = 1; y = 2
Do not use semicolons to terminate expressions. Open
) { puts MiGA::MiGA.FULL_VERSION; exit }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
Example:
# bad
foo = 1; bar = 2;
baz = 3;
# good
foo = 1
bar = 2
baz = 3
Put empty method definitions on a single line. Open
def parse_cli
end
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for the formatting of empty method definitions.
By default it enforces empty method definitions to go on a single
line (compact style), but it can be configured to enforce the end
to go on its own line (expanded style).
Note: A method definition is not considered empty if it contains comments.
Example: EnforcedStyle: compact (default)
# bad
def foo(bar)
end
def self.foo(bar)
end
# good
def foo(bar); end
def foo(bar)
# baz
end
def self.foo(bar); end
Example: EnforcedStyle: expanded
# bad
def foo(bar); end
def self.foo(bar); end
# good
def foo(bar)
end
def self.foo(bar)
end
Missing magic comment # frozen_string_literal: true
. Open
# @package MiGA
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop is designed to help upgrade to Ruby 3.0. It will add the
comment # frozen_string_literal: true
to the top of files to
enable frozen string literals. Frozen string literals may be default
in Ruby 3.0. The comment will be added below a shebang and encoding
comment. The frozen string literal comment is only valid in Ruby 2.3+.
Example: EnforcedStyle: when_needed (default)
# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
# ...
end
# good
# frozen_string_literal: true
module Foo
# ...
end
Example: EnforcedStyle: always
# 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
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
Unnecessary splat expansion. Open
when *%w[query ignore-dup get-metadata only-metadata]
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for unneeded usages of splat expansion
Example:
# bad
a = *[1, 2, 3]
a = *'a'
a = *1
begin
foo
rescue *[StandardError, ApplicationError]
bar
end
case foo
when *[1, 2, 3]
bar
else
baz
end
Example:
# good
c = [1, 2, 3]
a = *c
a, b = *c
a, *b = *c
a = *1..10
a = ['a']
begin
foo
rescue StandardError, ApplicationError
bar
end
case foo
when *[1, 2, 3]
bar
else
baz
end
Use %i
or %I
for an array of symbols. Open
cli.opt_object(opt, [:project, :dataset_opt])
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop can check for array literals made up of symbols that are not using the %i() syntax.
Alternatively, it checks for symbol arrays using the %i() syntax on projects which do not want to use that syntax.
Configuration option: MinSize
If set, arrays with fewer elements than this value will not trigger the
cop. For example, a MinSize of
3` will not enforce a style on an array
of 2 or fewer elements.
Example: EnforcedStyle: percent (default)
# good
%i[foo bar baz]
# bad
[:foo, :bar, :baz]
Example: EnforcedStyle: brackets
# good
[:foo, :bar, :baz]
# bad
%i[foo bar baz]
Use a guard clause instead of wrapping the code inside a conditional expression. Open
unless status&.success?
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
Example:
# bad
def test
if something
work
end
end
# good
def test
return unless something
work
end
# also good
def test
work if something
end
# bad
if something
raise 'exception'
else
ok
end
# good
raise 'exception' if something
ok
Do not use semicolons to terminate expressions. Open
) { |v| cli[:activate] = false ; cli[:reason] = v }
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
This cop checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
Example:
# bad
foo = 1; bar = 2;
baz = 3;
# good
foo = 1
bar = 2
baz = 3