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

[Fix #7928] Fix a false message for Style/GuardClause #7932

Merged
merged 1 commit into from May 10, 2020

Conversation

koic
Copy link
Member

@koic koic commented May 5, 2020

Fixes #7928.

This PR fixes a false message for Style/GuardClause when using and or or operators for guard clause in then or else branches.

The following is an example:

% cat example.rb
if cond
  foo || raise('hi')
else
  bar
end

Before

% bundle exec rubocop --only Style/GuardClause
(snip)
Offenses:

example.rb:1:1: C: Style/GuardClause: Use a guard clause (raise('hi') if cond) instead of wrapping the code inside a conditional expression.
if cond
^^

1 file inspected, 1 offense detected

After

% bundle exec rubocop --only Style/GuardClause
(snip)

Offenses:

example.rb:1:1: C: Style/GuardClause: Use a guard clause (foo || raise('hi') if cond) instead of wrapping the code inside a conditional
expression.
if cond
^^

1 file inspected, 1 offense detected

Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the fix_false_message_for_guard_clause branch 3 times, most recently from bce7a32 to 96bf727 Compare May 9, 2020 06:28
Fixes rubocop#7928.

This PR fixes a false message for `Style/GuardClause` when using
`and` or `or` operators for guard clause in `then` or `else` branches.

The following is an example:

```console
% cat example.rb
if cond
  foo || raise('hi')
else
  bar
end
```

## Before

```console
% bundle exec rubocop --only Style/GuardClause
(snip)
Offenses:

example.rb:1:1: C: Style/GuardClause: Use a guard clause (raise('hi') if
cond) instead of wrapping the code inside a conditional expression.
if cond
^^

1 file inspected, 1 offense detected
```

## After

```console
% bundle exec rubocop --only Style/GuardClause
(snip)

Offenses:

example.rb:1:1: C: Style/GuardClause: Use a guard clause (foo ||
raise('hi') if cond) instead of wrapping the code inside a conditional
expression.
if cond
^^

1 file inspected, 1 offense detected
```
@koic koic force-pushed the fix_false_message_for_guard_clause branch from 96bf727 to 839e3b0 Compare May 10, 2020 12:25
@koic koic merged commit 7d25177 into rubocop:master May 10, 2020
@koic koic deleted the fix_false_message_for_guard_clause branch May 10, 2020 13:02
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.

Style/GuardClause false positive
2 participants