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

Fix an incorrect autocorrect for Style/RedundantException when message is not string #12177

Conversation

ydah
Copy link
Member

@ydah ydah commented Sep 1, 2023

The following autocorrection is performed, but when the message is not a string, the behavior changes after the autocorrection.

# before
raise RuntimeError, Object.new
# => #<RuntimeError: #<Object:0x000000010f369da8>>

# offense
# spec/spec_helper.rb:3:1: C: [Correctable] Style/RedundantException: Redundant RuntimeError argument can be removed.
# raise RuntimeError, Object.new
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# after
raise Object.new
# => #<TypeError: exception class/object expected>

Therefore, this PR fixes so that the behavior does not change.

irb(main):001:0> e = raise Object.new rescue $!
=> #<TypeError: exception class/object expected>
irb(main):002:0> e = raise RuntimeError, Object.new rescue $!
=> #<RuntimeError: #<Object:0x000000010f369da8>>
irb(main):003:0> e = raise Object.new.to_s rescue $!
=> #<RuntimeError: #<Object:0x000000010f3402a0>>
irb(main):004:0> e = raise RuntimeError.new(Object.new) rescue $!
=> #<RuntimeError: #<Object:0x00000001049d3e88>>
irb(main):005:0> e = raise RuntimeError.new Object.new  rescue $!
=> #<RuntimeError: #<Object:0x00000001079dadc0>>

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@ydah ydah force-pushed the fix_an_incorrect_autocorrect_for_style_redundant_exception branch from 7cb83c1 to a4e49a1 Compare September 1, 2023 12:43
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 8, 2023

I'm guessing you can also mention this case in the cop's documentation examples.

@ydah ydah force-pushed the fix_an_incorrect_autocorrect_for_style_redundant_exception branch from a4e49a1 to ba9d3ff Compare September 11, 2023 14:35
@ydah
Copy link
Member Author

ydah commented Sep 11, 2023

I update this PR. Thank you for review.

@ydah
Copy link
Member Author

ydah commented Oct 11, 2023

Please point out if there is anything in this PR that is still a blocker.

@bbatsov bbatsov merged commit 873cb9f into rubocop:master Oct 11, 2023
28 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 11, 2023

It's fine, I just totally forgot about. Sorry about the delay!

@ydah ydah deleted the fix_an_incorrect_autocorrect_for_style_redundant_exception branch October 11, 2023 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants