Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive Lint/OrderedMagicComments warning #12213

Closed
DmitryPogrebnoy opened this issue Sep 21, 2023 · 0 comments · Fixed by #12218
Closed

False-positive Lint/OrderedMagicComments warning #12213

DmitryPogrebnoy opened this issue Sep 21, 2023 · 0 comments · Fixed by #12218
Labels

Comments

@DmitryPogrebnoy
Copy link

There is a specific case when the Lint/OrderedMagicComments cop produces a false-positive warning.

Here is an example:

# frozen_string_literal: true

# Example:
#
#   re = eval("# encoding: ISO-8859-1\n/foo \\xff?/")
#   re.encoding                 # => #<Encoding:ISO-8859-1>
#   re =~ "foo".encode("UTF-8") # => 0
#   re =~ "foo\u0100"           # Raises Encoding::CompatibilityError

Expected behavior

There is no warning.

Actual behavior

The Lint/OrderedMagicComments warning is present.

Here is an output rubocop --debug:

For /Users/dmitry.pogrebnoy/RubymineProjects/untitled25: Default configuration from /Users/dmitry.pogrebnoy/.rvm/gems/ruby-3.3.0-preview2-debug/gems/rubocop-1.56.3/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/dmitry.pogrebnoy/RubymineProjects/untitled25/1.rb
W

Offenses:

1.rb:5:1: W: [Correctable] Lint/OrderedMagicComments: The encoding magic comment should precede all other magic comments.
#   re = eval("# encoding: ISO-8859-1\n/foo \\xff?/")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable
Finished in 0.18397099990397692 seconds

Steps to reproduce the problem

  1. Create empty ruby file
  2. Copy following example to file.
# frozen_string_literal: true

# Example:
#
#   re = eval("# encoding: ISO-8859-1\n/foo \\xff?/")
#   re.encoding                 # => #<Encoding:ISO-8859-1>
#   re =~ "foo".encode("UTF-8") # => 0
#   re =~ "foo\u0100"           # Raises Encoding::CompatibilityError
  1. Run rubocop on that file.

RuboCop version

1.56.3 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.3.0) [arm64-darwin22]

@DmitryPogrebnoy DmitryPogrebnoy changed the title False-positive Lint/OrderedMagicComments False-positive Lint/OrderedMagicComments warning Sep 21, 2023
@koic koic added the bug label Sep 22, 2023
koic added a commit to koic/rubocop that referenced this issue Sep 24, 2023
Fixes rubocop#12213.

This PR fixes a false positive for `Lint/OrderedMagicComments`
when comment text `# encoding: ISO-8859-1` is embedded within example code as source code comment.
koic added a commit that referenced this issue Sep 25, 2023
…ered_magic_comments

[Fix #12213] Fix a false positive for `Lint/OrderedMagicComments`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants