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

Add Lint/NoReturnInMemoization cop #699

Open
wants to merge 5 commits into
base: rm-lint-department
Choose a base branch
from

Conversation

rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 28, 2025

Cop based on https://docs.rubocop.org/rubocop/cops_lint.html#lintnoreturninbeginendblocks, but with the difference that it only cares about instance variables, given sometimes using return inside begin..end blocks is idiomatic Ruby, like when handling exceptions.

Example of such case:

def some_method
  some_value = begin
    get_external_api
  rescue ApiError
    return { error: "Api returned an error" }
  end

  do_something(some_value)
end

This cop checks for the use of return with a value in begin..end blocks in the context of instance variable assignment such as memoization. Using return with a value in these blocks can lead to unexpected behavior as the return will exit the method and not set the value of the instance variable.

# bad
def foo
  @foo ||= begin
    return 1 if bar
    2
  end
end

# bad
def foo
  @foo = begin
    return 1 if bar
    2
  end
end

# bad
def foo
  @foo += begin
    return 1 if bar
    2
  end
end

# bad - using return in rescue blocks still exits the method
def foo
  @foo ||= begin
    bar
  rescue
    return 2 if baz
  end
end

# good
def foo
  @foo ||= begin
    if bar
      1
    else
      2
    end
  end
end

# good - not an assignment to an instance variable
def foo
  foo = begin
    return 1 if bar
    2
  end
end

# good - proper exception handling without return
def foo
  @foo ||= begin
    bar
  rescue
    baz ? 2 : 3
  end
end

@rafaelfranca rafaelfranca requested a review from a team as a code owner February 28, 2025 17:28
Copy link

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@github-actions github-actions bot added the config change Changes the Rubocop config by enabling, disabling, or reconfiguring one or many cops label Mar 3, 2025
This cop checks for the use of `return` with a value in `begin..end` blocks
in the context of instance variable assignment such as memoization. Using
`return` with a value in these blocks can lead to unexpected behavior as
the `return` will exit the method and not set the value of the instance
variable.
We only support Rubocop 1.72.
Now we have Lint/NoReturnInMemoization cop that should replace this one.
Make explicit that this is going to be a major version bump.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config change Changes the Rubocop config by enabling, disabling, or reconfiguring one or many cops
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants