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

Discourage the use of writer methods with rescued exceptions #957

Open
viralpraxis opened this issue Dec 16, 2024 · 0 comments
Open

Discourage the use of writer methods with rescued exceptions #957

viralpraxis opened this issue Dec 16, 2024 · 0 comments

Comments

@viralpraxis
Copy link

ref: rubocop/rubocop#13588

This is a little-known (I believe) and rarely used feature of the rescue operator, which allows assigning an exception using an object's writer method:

Foo = Struct.new(:exception)
foo = Foo.new

begin
  do_something_that_might_raise
rescue => foo.exception # `Foo#exception=` will be called
  Rails.error.report(foo.exception)
  do_something_with_exception(foo.exception)
end

I propose introducing a new cop that detects such patterns and suggests expanding them to

Foo = Struct.new(:exception)
foo = Foo.new

begin
  do_something_that_might_raise
rescue => e
  foo.exception = e # if required
  Rails.error.report(e)
  do_something_with_exception(e)
end

This cop could also support an alternative style that, conversely, suggests using the writer variant (where applicable).

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

No branches or pull requests

1 participant