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 an incorrect autocorrect for Style/MapToHash with Layout/SingleLineBlockChain #12407

Conversation

koic
Copy link
Member

@koic koic commented Nov 24, 2023

This PR fixes an incorrect autocorrect for Style/MapToHash with Layout/SingleLineBlockChain, which makes the following syntax error occur:

$ cat example.rb
obj.map { |i| foo(i) }.to_h
   .bar

$ bundle exec rubocop -A example.rb --only Style/MapToHash,Layout/SingleLineBlockChain
Inspecting 1 file
F

Offenses:

example.rb:1:5: C: [Corrected] Style/MapToHash: Pass a block to to_h instead of calling map.to_h.
obj.map { |i| foo(i) }.to_h
    ^^^
example.rb:1:23: C: [Corrected] Layout/SingleLineBlockChain: Put method call on a separate line if chained to a single line block.
obj.map { |i| foo(i) }.to_h
                      ^^^^^
example.rb:3:4: F: Lint/Syntax: unexpected token tDOT
(Using Ruby 3.3 parser; configure using TargetRubyVersion parameter, under AllCops)
   .bar
   ^

1 file inspected, 3 offenses detected, 2 offenses corrected

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.

…LineBlockChain`

This PR fixes an incorrect autocorrect for `Style/MapToHash` with `Layout/SingleLineBlockChain`
which makes the following syntax error occur:

```console
$ cat example.rb
obj.map { |i| foo(i) }.to_h
   .bar

$ bundle exec rubocop -A example.rb --only Style/MapToHash,Layout/SingleLineBlockChain
Inspecting 1 file
F

Offenses:

example.rb:1:5: C: [Corrected] Style/MapToHash: Pass a block to to_h instead of calling map.to_h.
obj.map { |i| foo(i) }.to_h
    ^^^
example.rb:1:23: C: [Corrected] Layout/SingleLineBlockChain: Put method call on a separate line if chained to a single line block.
obj.map { |i| foo(i) }.to_h
                      ^^^^^
example.rb:3:4: F: Lint/Syntax: unexpected token tDOT
(Using Ruby 3.3 parser; configure using TargetRubyVersion parameter, under AllCops)
   .bar
   ^

1 file inspected, 3 offenses detected, 2 offenses corrected
```
@koic koic changed the title Fix an incorrect autocorrect for Layout/SingleLineBlockChain with Style/MapToHash Fix an incorrect autocorrect for Style/MapToHash with Layout/SingleLineBlockChain Nov 24, 2023
@koic koic force-pushed the fix_incorrect_autocorrect_for_layout_single_line_block_chain_with_style_map_to_hash branch from bb4a13f to 84da432 Compare November 24, 2023 03:47
@ydakuka
Copy link

ydakuka commented Nov 24, 2023

Another example for the false-negative on the Style/MapToHash:

hsh = [['a', '1'], ['b', '2']]
# hsh = nil

# bad
hsh&.map { |k, v| [k, v] }&.to_h

# good
hsh&.to_h { |k,v| [k, v] }

@koic
Copy link
Member Author

koic commented Nov 24, 2023

That issue is different from the intended fix of this PR, so I've opened a separate PR #12408. Thank you.

koic added a commit to koic/rubocop that referenced this pull request Nov 24, 2023
Follow up rubocop#12407 (comment).

This PR makes `Style/MapToHash` aware of safe navigation operator.
@koic koic merged commit 09cba51 into rubocop:master Nov 24, 2023
28 checks passed
@koic koic deleted the fix_incorrect_autocorrect_for_layout_single_line_block_chain_with_style_map_to_hash branch November 24, 2023 13:56
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