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 a false negative for Layout/ExtraSpacing #12043

Merged

Conversation

koic
Copy link
Member

@koic koic commented Jul 12, 2023

This PR fixes a false negative for Layout/ExtraSpacing when some character are vertically aligned.

For example, there is a wasted space before the do keyword.

d_is_vertically_aligned  do
  _______________________d
end

The d char is accidentally vertically aligned and cannot be detected:

$ bundle exec rubocop --only Layout/ExtraSpacing
(snip)

1 file inspected, no offenses detected

Looking at the test code, it looks like this is to allow vertical alignment of dots in method calls:

y, m = (year * 12 + (mon - 1) + n).divmod(12)
m,   = (m + 1)                    .divmod(1)

https://github.com/rubocop/rubocop/blob/v1.54.1/spec/rubocop/cop/layout/extra_spacing_spec.rb#L172-L177

Therefore, this PR tweaks the alignment logic to be closer to the expected behavior.

And it also suppresses the following new offenses found with this tweak:

$ bundle exec rubocop
(snip)

lib/rubocop/string_interpreter.rb:37:18: C: [Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.
        when /\d/       then interpret_octal(escape)
                 ^^^^^^
spec/rubocop/target_finder_spec.rb:74:35: C: [Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.
    create_file('dir1/executable',  '#!/usr/bin/env ruby')
                                  ^

1513 files inspected, 3 offenses detected, 3 offenses autocorrectable

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 a false negative for `Layout/ExtraSpacing` when some character are vertically aligned.

For example, there is a wasted space before the `do` keyword.

```ruby
d_is_vertically_aligned  do
  _______________________d
end
```

The `d` char is accidentally vertically aligned and cannot be detected:

```console
$ bundle exec rubocop --only Layout/ExtraSpacing
(snip)

1 file inspected, no offenses detected
```

Looking at the test code, it looks like this is to allow vertical alignment of dots in method calls:

```ruby
y, m = (year * 12 + (mon - 1) + n).divmod(12)
m,   = (m + 1)                    .divmod(1)
```

https://github.com/rubocop/rubocop/blob/v1.54.1/spec/rubocop/cop/layout/extra_spacing_spec.rb#L172-L177

Therefore, this PR tweaks the alignment logic to be closer to the expected behavior.

And it also suppresses the following new offenses found with this tweak:

```console
$ bundle exec rubocop
(snip)

lib/rubocop/string_interpreter.rb:37:18: C: [Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.
        when /\d/       then interpret_octal(escape)
                 ^^^^^^
spec/rubocop/target_finder_spec.rb:74:35: C: [Correctable] Layout/ExtraSpacing: Unnecessary spacing detected.
    create_file('dir1/executable',  '#!/usr/bin/env ruby')
                                  ^

1513 files inspected, 3 offenses detected, 3 offenses autocorrectable
```
@bbatsov bbatsov merged commit 02c32b4 into rubocop:master Jul 12, 2023
28 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 12, 2023

Looks good. Thanks!

@koic koic deleted the fix_a_false_negative_for_layout_extra_spacing branch July 12, 2023 04:28
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

2 participants