Missing frozen string literal comment. Open
class MaxBytes < Grape::Validations::Validators::Base
- 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
Use a guard clause (return unless params[attr_name].bytesize > max_bytes
) instead of wrapping the code inside a conditional expression. Open
if params[attr_name].bytesize > max_bytes
- Read upRead up
- Exclude checks
Use a guard clause instead of wrapping the code inside a conditional expression
A condition with an elsif
or else
branch is allowed unless
one of return
, break
, next
, raise
, or fail
is used
in the body of the conditional expression.
NOTE: Autocorrect works in most cases except with if-else statements
that contain logical operators such as foo || raise('exception')
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
# bad
if something
foo || raise('exception')
else
ok
end
# good
foo || raise('exception') if something
ok
Example: AllowConsecutiveConditionals: false (default)
# bad
def test
if foo?
work
end
if bar? # <- reports an offense
work
end
end
Example: AllowConsecutiveConditionals: true
# good
def test
if foo?
work
end
if bar?
work
end
end
# bad
def test
if foo?
work
end
do_something
if bar? # <- reports an offense
work
end
end