Skip to content

Conversation

@Earlopain
Copy link
Contributor

Followup to 501e5d3

This type of autocorrect can only happen if the method contains no other code. Otherwise, it would exist early if followed by other code. Docs only contained example code for it happening inside of a method in that way, but the cop actually checks for the pattern regardless of context. In such cases, the longer form of autocorrect must happen.

Ref #1513


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.
expect_correction(<<~RUBY)
def foo
if defined?(@current_user)
@current_user
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indentation is wrong here but that was already happening and other cops will take care of it.

Copy link
Member

Choose a reason for hiding this comment

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

I see. It’s fine to delegate this to another cop.

RUBY

expect_correction(<<~RUBY)
return @current_user if defined?(@current_user)
Copy link
Member

Choose a reason for hiding this comment

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

Overall, this looks good. However, when right_siblings is empty, wouldn’t it be simpler to return early like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a bit more complicated than that I think. In a block for example it should also not return early if code follows:

foo do @bar ||= User.find_by(id: id) end something else 

There are some cases where you can do that, but I'm not certain yet which that would be. I think the way it is it should already handle the majority.

Copy link
Member

@koic koic Aug 13, 2025

Choose a reason for hiding this comment

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

Yeah, either way, this will definitely fix the mistake. Thank you!

@koic koic merged commit 812d404 into rubocop:master Aug 13, 2025
16 checks passed
@Earlopain
Copy link
Contributor Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants