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

Lint/NestedMethodDefinition false alarm on the @ presence #12960

Closed
yegor256 opened this issue Jun 4, 2024 · 1 comment · Fixed by #12961
Closed

Lint/NestedMethodDefinition false alarm on the @ presence #12960

yegor256 opened this issue Jun 4, 2024 · 1 comment · Fixed by #12961
Labels

Comments

@yegor256
Copy link

yegor256 commented Jun 4, 2024

I'm doing this:

class F
  def foo
    @x = Object.new
    def @x.bar(_t)
      true
    end
  end
end

Rubocop 1.64.0 complains:

a.rb:4:5: W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use lambda instead.
    def @x.bar(_t) ...
    ^^^^^^^^^^^^^^

Looks like a bug.

More details here: https://stackoverflow.com/questions/78573984/how-to-define-a-ruby-method-in-runtime-so-that-rubocop-doesnt-complain?noredirect=1#comment138524046_78573984

@koic koic added the bug label Jun 4, 2024
koic added a commit to koic/rubocop that referenced this issue Jun 4, 2024
…ion`

Fixes rubocop#12960.

This PR fixes false positives for `Lint/NestedMethodDefinition` when definition of method on variable.

Since method definitions using local variables such as `def obj.y` have already been accepted,
the decision is to similarly accept method definitions using other variable types:
https://github.com/rubocop/rubocop/blob/v1.64.1/spec/rubocop/cop/lint/nested_method_definition_spec.rb#L80-L89
bbatsov pushed a commit that referenced this issue Jun 4, 2024
Fixes #12960.

This PR fixes false positives for `Lint/NestedMethodDefinition` when definition of method on variable.

Since method definitions using local variables such as `def obj.y` have already been accepted,
the decision is to similarly accept method definitions using other variable types:
https://github.com/rubocop/rubocop/blob/v1.64.1/spec/rubocop/cop/lint/nested_method_definition_spec.rb#L80-L89
@yegor256
Copy link
Author

yegor256 commented Jun 4, 2024

@koic thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants