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

Style/TopLevelMethodDefinition misses methods declared after a class. #9819

Closed
nertzy opened this issue May 24, 2021 · 0 comments · Fixed by #9820
Closed

Style/TopLevelMethodDefinition misses methods declared after a class. #9819

nertzy opened this issue May 24, 2021 · 0 comments · Fixed by #9820
Labels

Comments

@nertzy
Copy link

nertzy commented May 24, 2021

See my gist for all relevant files and output.

Expected behavior

Style/TopLevelMethodDefinition should detect top-level methods regardless of where they appear in a file, including after a class definition.

Actual behavior

Describe here what actually happened.
Please use rubocop --debug when pasting rubocop output as it contains additional information.

Steps to reproduce the problem

Turn on the Style/TopLevelMethodDefinition cop:

Style/TopLevelMethodDefinition:
  Enabled: true

Run rubocop against the following file:

class One
  def two
    2
  end
end

def one
  1
end

The top-level method one will not get detected.

Note that the cop works if the method is the only contents of the file:

def one
  1
end

RuboCop version

❯ bundle exec rubocop -V
1.15.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 2.7.3 x86_64-darwin20)
@koic koic added the bug label May 24, 2021
koic added a commit to koic/rubocop that referenced this issue May 24, 2021
…nition`

Fixes rubocop#9819.

This PR fixes a false negative for `Style/TopLevelMethodDefinition`
when defining a top-level method after a class definition.
bbatsov pushed a commit that referenced this issue May 25, 2021
Fixes #9819.

This PR fixes a false negative for `Style/TopLevelMethodDefinition`
when defining a top-level method after a class definition.
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