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 simple autocorrect for Style/GuardClause #10255

Merged
merged 1 commit into from Oct 28, 2022

Conversation

FnControlOption
Copy link
Contributor

@FnControlOption FnControlOption commented Nov 17, 2021

This adds autocorrection for Style/GuardClause. For example:

def test
  if foo1
    return foo2
  else
    foo3
  end

  if bar1
    bar2
    bar3
  end
end

becomes

def test
  return foo2 if foo1

  foo3

  return unless bar1

  bar2
  bar3
end

More complex if-else statements are not autocorrected and left as-is:

if foo
  bar || return
else
  baz
end

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.

CHANGELOG.md Outdated Show resolved Hide resolved
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 6, 2021

The proposal looks reasonable to me. Might be a good idea to explain in the cop docs the scope of the auto-correct, so people do not have to wonder how it works exactly.

@dvandersluis
Copy link
Member

@FnControlOption are you still interested in completing this PR?

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 29, 2022

@FnControlOption Please, rebase on top of master and squash your commits together.

@bbatsov bbatsov merged commit 9228ed3 into rubocop:master Oct 28, 2022
koic added a commit that referenced this pull request Oct 28, 2022
Follow up #10255.

This commit fixes the following build error.

```console
==> Failures

  1) RuboCop Project Changelog future entries For /Users/koic/src/github.com/rubocop/rubocop/spec/..
     /changelog/new_add_simple_autocorrect_for_style_guard_clause.md has a link to the issue or
     pull request address at the beginning
     Failure/Error: expect(entries).to all(match(address_pattern))

       expected ["* Add simple autocorrect for `Style/GuardClause`. ([@FnControlOption][])"] to
       all match /\A\* \[#\d+\]\(https:\/\/github\.com\/rubocop\/rubocop\/(issues|pull)\/\d+\):/
          object at index 0 failed to match:
            expected "* Add simple autocorrect for `Style/GuardClause`. ([@FnControlOption][])" to
            match /\A\* \[#\d+\]\(https:\/\/github\.com\/rubocop\/rubocop\/(issues|pull)\/\d+\):/
     # ./spec/project_spec.rb:254:in `block (6 levels) in <top (required)>'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants