rubocop how do you fix Missing magic comment
Try running Rubocop with the -D
option:
rubocop -D
Inspecting 1 file
C
Offenses:
spec/rails_helper.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
require 'spec_helper'
^
Adding -D
will cause Rubocop to print the name of the cop that was violated, in this case Style/FrozenStringLiteralComment
. You can then search for that cop in the Rubocop documentation:
http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FrozenStringLiteralComment
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 will 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+.
If you want to ignore it, add to your .rubocop.yml
Style/FrozenStringLiteralComment:
Enabled: false
But may be you want to know what "Magic comment" is, especially if you're using Ruby 2.x
Just add
# frozen_string_literal: true
to the first line of each Ruby file. Or run
rubocop -a
to allow Rubocop to fix all offenses automatically that it is able to fix.
Btw. I like Rubocop and use it myself, but I wouldn't call the things it finds defects. I see the list more like suggestions or reasons for a discussion with my co-workers.
If you want to be more specific and run rubocop
for only # frozen_string_literal: true
you can use the --only
flag option:
Run only the specified cop(s) and/or cops in the specified departments.
To view those files:
rubocop --only Style/FrozenStringLiteralComment
To autocorrect those specific files use the -A
flag (as mentioned in a previous answer):
rubocop --only Style/FrozenStringLiteralComment -A
You can view more information about autocorrect here.