lib/cc/analyzer/measurement_validations/name_validation.rb
Redundant escape inside regexp literal Open
Open
REGEX = /^[A-Za-z0-9_\.\-]+$/
- Read upRead up
- Exclude checks
Checks for redundant escapes inside Regexp literals.
Example:
# bad
%r{foo\/bar}
# good
%r{foo/bar}
# good
/foo\/bar/
# good
%r/foo\/bar/
# good
%r!foo\!bar!
# bad
/a\-b/
# good
/a-b/
# bad
/[\+\-]\d/
# good
/[+\-]\d/
Redundant safe navigation detected. Open
Open
name&.is_a?(String) && REGEX.match?(name)
- Read upRead up
- Exclude checks
Checks for redundant safe navigation calls.
instance_of?
, kind_of?
, is_a?
, eql?
, respond_to?
, and equal?
methods
are checked by default. These are customizable with AllowedMethods
option.
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
do_something if attrs&.respond_to?(:[])
# good
do_something if attrs.respond_to?(:[])
# bad
while node&.is_a?(BeginNode)
node = node.parent
end
# good
while node.is_a?(BeginNode)
node = node.parent
end
# good - without `&.` this will always return `true`
foo&.respond_to?(:to_a)