Showing 53 of 53 total issues
Avoid too many return
statements within this method. Open
return remains += args[i + 1..-1]
Avoid too many return
statements within this method. Open
return remains += args[i..-1]
Method registry_getopttype
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def self.registry_getopttype(type)
return nil unless type
if type.respond_to?(:name)
type = type.name
lookup = type.downcase.to_sym
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method die
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def die(arg, msg = nil, error_code = nil)
msg, error_code = nil, msg if msg.kind_of?(Integer)
if msg
$stderr.puts "Error: argument --#{@specs[arg].long} #{msg}."
else
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method handle_short_opt
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
def self.handle_short_opt(sopt)
sopt = sopt.to_s if sopt && sopt != :none
sopt = case sopt
when /^-(.)$/ then $1
when nil, :none, /^.$/ then sopt
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Use string as argument instead of regexp. Open
shortargs = $1.split(//)
- Exclude checks
Use match?
instead of =~
when MatchData
is not used. Open
sym = nil if arg =~ /--no-/ # explicitly invalidate --no-no- arguments
- Read upRead up
- Exclude checks
In Ruby 2.4, String#match?
, Regexp#match?
and Symbol#match?
have been added. The methods are faster than match
.
Because the methods avoid creating a MatchData
object or saving
backref.
So, when MatchData
is not used, use match?
instead of match
.
Example:
# bad
def foo
if x =~ /re/
do_something
end
end
# bad
def foo
if x.match(/re/)
do_something
end
end
# bad
def foo
if /re/ === x
do_something
end
end
# good
def foo
if x.match?(/re/)
do_something
end
end
# good
def foo
if x =~ /re/
do_something(Regexp.last_match)
end
end
# good
def foo
if x.match(/re/)
do_something($~)
end
end
# good
def foo
if /re/ === x
do_something($~)
end
end
Use match?
instead of =~
when MatchData
is not used. Open
if param =~ /^(stdin|-)$/i
- Read upRead up
- Exclude checks
In Ruby 2.4, String#match?
, Regexp#match?
and Symbol#match?
have been added. The methods are faster than match
.
Because the methods avoid creating a MatchData
object or saving
backref.
So, when MatchData
is not used, use match?
instead of match
.
Example:
# bad
def foo
if x =~ /re/
do_something
end
end
# bad
def foo
if x.match(/re/)
do_something
end
end
# bad
def foo
if /re/ === x
do_something
end
end
# good
def foo
if x.match?(/re/)
do_something
end
end
# good
def foo
if x =~ /re/
do_something(Regexp.last_match)
end
end
# good
def foo
if x.match(/re/)
do_something($~)
end
end
# good
def foo
if /re/ === x
do_something($~)
end
end
Check block argument explicitly instead of using block_given?
. Open
opts[:callback] ||= b if block_given?
- Exclude checks
Unused block argument - val
. If it's necessary, use _
or _val
as an argument name to indicate that it won't be used. Open
required.each do |sym, val|
- Read upRead up
- Exclude checks
Checks for unused block arguments.
Example:
# bad
do_something do |used, unused|
puts used
end
do_something do |bar|
puts :foo
end
define_method(:foo) do |bar|
puts :baz
end
# good
do_something do |used, _unused|
puts used
end
do_something do
puts :foo
end
define_method(:foo) do |_bar|
puts :baz
end
Example: IgnoreEmptyBlocks: true (default)
# good
do_something { |unused| }
Example: IgnoreEmptyBlocks: false
# bad
do_something { |unused| }
Example: AllowUnusedKeywordArguments: false (default)
# bad
do_something do |unused: 42|
foo
end
Example: AllowUnusedKeywordArguments: true
# good
do_something do |unused: 42|
foo
end
Useless private
access modifier. Open
private
- Read upRead up
- Exclude checks
Checks for redundant access modifiers, including those with no
code, those which are repeated, and leading public
modifiers in a
class or module body. Conditionally-defined methods are considered as
always being defined, and thus access modifiers guarding such methods
are not redundant.
This cop has ContextCreatingMethods
option. The default setting value
is an empty array that means no method is specified.
This setting is an array of methods which, when called, are known to
create its own context in the module's current access context.
It also has MethodCreatingMethods
option. The default setting value
is an empty array that means no method is specified.
This setting is an array of methods which, when called, are known to
create other methods in the module's current access context.
Example:
# bad
class Foo
public # this is redundant (default access is public)
def method
end
end
# bad
class Foo
# The following is redundant (methods defined on the class'
# singleton class are not affected by the private modifier)
private
def self.method3
end
end
# bad
class Foo
protected
define_method(:method2) do
end
protected # this is redundant (repeated from previous modifier)
[1,2,3].each do |i|
define_method("foo#{i}") do
end
end
end
# bad
class Foo
private # this is redundant (no following methods are defined)
end
# good
class Foo
private # this is not redundant (a method is defined)
def method2
end
end
# good
class Foo
# The following is not redundant (conditionally defined methods are
# considered as always defining a method)
private
if condition?
def method
end
end
end
# good
class Foo
protected # this is not redundant (a method is defined)
define_method(:method2) do
end
end
Example: ContextCreatingMethods: concerning
# Lint/UselessAccessModifier:
# ContextCreatingMethods:
# - concerning
# good
require 'active_support/concern'
class Foo
concerning :Bar do
def some_public_method
end
private
def some_private_method
end
end
# this is not redundant because `concerning` created its own context
private
def some_other_private_method
end
end
Example: MethodCreatingMethods: delegate
# Lint/UselessAccessModifier:
# MethodCreatingMethods:
# - delegate
# good
require 'active_support/core_ext/module/delegation'
class Foo
# this is not redundant because `delegate` creates methods
private
delegate :method_a, to: :method_b
end
Do not set test_files
in gemspec. Open
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
- Read upRead up
- Exclude checks
Checks that deprecated attributes are not set in a gemspec file. Removing deprecated attributes allows the user to receive smaller packed gems.
Example:
# bad
Gem::Specification.new do |spec|
spec.name = 'your_cool_gem_name'
spec.test_files = Dir.glob('test/**/*')
end
# bad
Gem::Specification.new do |spec|
spec.name = 'your_cool_gem_name'
spec.test_files += Dir.glob('test/**/*')
end
# good
Gem::Specification.new do |spec|
spec.name = 'your_cool_gem_name'
end
Useless assignment to variable - lopt
. Open
lopt = case lopt
- Read upRead up
- Exclude checks
Checks for every useless assignment to local variable in every
scope.
The basic idea for this cop was from the warning of ruby -cw
:
assigned but unused variable - foo
Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.
NOTE: Given the assignment foo = 1, bar = 2
, removing unused variables
can lead to a syntax error, so this case is not autocorrected.
Safety:
This cop's autocorrection is unsafe because removing assignment from
operator assignment can cause NameError if this assignment has been used to declare
local variable. For example, replacing a ||= 1
to a || 1
may cause
"undefined local variable or method `a' for main:Object (NameError)".
Example:
# bad
def some_method
some_var = 1
do_something
end
Example:
# good
def some_method
some_var = 1
do_something(some_var)
end
private
(on line 702) does not make singleton methods private. Use private_class_method
or private
inside a class << self
block instead. Open
def self.handle_long_opt(lopt, name)
- Read upRead up
- Exclude checks
Checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end
Use tr
instead of gsub
. Open
lopt = lopt ? lopt.to_s : name.to_s.gsub("_", "-")
- Read upRead up
- Exclude checks
This cop identifies places where gsub
can be replaced by
tr
or delete
.
Example:
# bad
'abc'.gsub('b', 'd')
'abc'.gsub('a', '')
'abc'.gsub(/a/, 'd')
'abc'.gsub!('a', 'd')
# good
'abc'.gsub(/.*/, 'a')
'abc'.gsub(/a+/, 'd')
'abc'.tr('b', 'd')
'a b c'.delete(' ')
The use of Kernel#open
is a serious security risk. Open
open param
- Read upRead up
- Exclude checks
Checks for the use of Kernel#open
and URI.open
with dynamic
data.
Kernel#open
and URI.open
enable not only file access but also process
invocation by prefixing a pipe symbol (e.g., open("| ls")
).
So, it may lead to a serious security risk by using variable input to
the argument of Kernel#open
and URI.open
. It would be better to use
File.open
, IO.popen
or URI.parse#open
explicitly.
NOTE: open
and URI.open
with literal strings are not flagged by this
cop.
Safety:
This cop could register false positives if open
is redefined
in a class and then used without a receiver in that class.
Example:
# bad
open(something)
open("| #{something}")
URI.open(something)
# good
File.open(something)
IO.popen(something)
URI.parse(something).open
# good (literal strings)
open("foo.text")
open("| foo")
URI.open("http://example.com")
Use bind_call(self, *a)
instead of bind(self).call(*a)
. Open
cloaker(&b).bind(self).call(*a) if b
- Exclude checks
Avoid more than 3 levels of block nesting. Open
unless num_params_taken
short_remaining << a
if @stop_on_unknown
remains << "-#{short_remaining}"
return remains += args[i + 1..-1]
- Read upRead up
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
Avoid more than 3 levels of block nesting. Open
unless yield "-#{a}", []
short_remaining << a
if @stop_on_unknown
short_remaining += shortargs[j + 1..-1].join
remains << "-#{short_remaining}"
- Read upRead up
- Exclude checks
Checks for excessive nesting of conditional and looping constructs.
You can configure if blocks are considered using the CountBlocks
option. When set to false
(the default) blocks are not counted
towards the nesting level. Set to true
to count blocks as well.
The maximum level of nesting allowed is configurable.
private
(on line 702) does not make singleton methods private. Use private_class_method
or private
inside a class << self
block instead. Open
def self.handle_short_opt(sopt)
- Read upRead up
- Exclude checks
Checks for private
or protected
access modifiers which are
applied to a singleton method. These access modifiers do not make
singleton methods private/protected. private_class_method
can be
used for that.
Example:
# bad
class C
private
def self.method
puts 'hi'
end
end
Example:
# good
class C
def self.method
puts 'hi'
end
private_class_method :method
end
Example:
# good
class C
class << self
private
def method
puts 'hi'
end
end
end