Skip to content

Fix a false positive for Style/BlockDelimiters #13268

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

Merged

Conversation

koic
Copy link
Member

@koic koic commented Sep 28, 2024

This PR fixes the following false positive for Style/BlockDelimiters when a single line do-end block with an inline rescue with a semicolon before rescue:

$ echo "foo do next unless bar; rescue StandardError; end" | bundle exec rubocop --stdin dummy.rb -A --only Style/BlockDelimiters
Inspecting 1 file
F

Offenses:

dummy.rb:1:5: C: [Corrected] Style/BlockDelimiters: Prefer {...} over do...end for single-line blocks.
foo do next unless bar; rescue StandardError; end
    ^^
dummy.rb:1:24: F: Lint/Syntax: unexpected token kRESCUE
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
foo { next unless bar; rescue StandardError; }
                       ^^^^^^
dummy.rb:1:46: F: Lint/Syntax: unexpected token tRCURLY
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
foo { next unless bar; rescue StandardError; }
                                             ^

1 file inspected, 3 offenses detected, 1 offense corrected
====================
foo { next unless bar; rescue StandardError; }

The following code is a syntax error when detected:

foo { next unless bar; rescue StandardError; }` # syntax error

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.

This PR fixes the following false positive for `Style/BlockDelimiters`
when a single line do-end block with an inline `rescue` with a semicolon before `rescue`:

```console
$ echo "foo do next unless bar; rescue StandardError; end" | bundle exec rubocop --stdin dummy.rb -A --only Style/BlockDelimiters
Inspecting 1 file
F

Offenses:

dummy.rb:1:5: C: [Corrected] Style/BlockDelimiters: Prefer {...} over do...end for single-line blocks.
foo do next unless bar; rescue StandardError; end
    ^^
dummy.rb:1:24: F: Lint/Syntax: unexpected token kRESCUE
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
foo { next unless bar; rescue StandardError; }
                       ^^^^^^
dummy.rb:1:46: F: Lint/Syntax: unexpected token tRCURLY
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
foo { next unless bar; rescue StandardError; }
                                             ^

1 file inspected, 3 offenses detected, 1 offense corrected
====================
foo { next unless bar; rescue StandardError; }
```

The following code is a syntax error when detected:

```ruby
foo { next unless bar; rescue StandardError; }` # syntax error
```
@koic koic merged commit d8c3f63 into rubocop:master Sep 28, 2024
22 checks passed
@koic koic deleted the fix_false_positive_for_style_block_delimiters branch September 28, 2024 15:45
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.

1 participant