diff --git a/changelog/fix_an_error_for_style_encoding.md b/changelog/fix_an_error_for_style_encoding.md new file mode 100644 index 000000000000..e6985209a63c --- /dev/null +++ b/changelog/fix_an_error_for_style_encoding.md @@ -0,0 +1 @@ +* [#12738](https://github.com/rubocop/rubocop/issues/12738): Fix an error for `Style/Encoding` when magic encoding with mixed case present. ([@koic][]) diff --git a/lib/rubocop/magic_comment.rb b/lib/rubocop/magic_comment.rb index d5c3843e4b28..7e919e5d8a84 100644 --- a/lib/rubocop/magic_comment.rb +++ b/lib/rubocop/magic_comment.rb @@ -268,7 +268,7 @@ def encoding # Rewrite the comment without a given token type def without(type) - if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/) + if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/io) '' else @comment diff --git a/spec/rubocop/cop/style/encoding_spec.rb b/spec/rubocop/cop/style/encoding_spec.rb index 6b5681d166b2..7be0cdd57923 100644 --- a/spec/rubocop/cop/style/encoding_spec.rb +++ b/spec/rubocop/cop/style/encoding_spec.rb @@ -33,6 +33,15 @@ def foo() end RUBY end + it 'registers an offense when magic encoding with mixed case present' do + expect_offense(<<~RUBY) + # Encoding: UTF-8 + ^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment. + RUBY + + expect_correction('') + end + it 'registers an offense when encoding present on 2nd line after shebang' do expect_offense(<<~RUBY) #!/usr/bin/env ruby