rubocop / rubocop/ruby-style-guide

Discourage the use of writer methods with rescued exceptions

Open
#957 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
16.5k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

ref: https://github.com/rubocop/rubocop/issues/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).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced RuboCop issue 13588 and the two rescue examples in this issue. Clarify whether the proposed cop should support both writer and non-writer styles, then identify the appropriate RuboCop implementation and test locations. Done means the behavior and preferred style are specified and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.