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

Style/RedundantDoubleSplatHashBraces incorrect Autofix #12293

Closed
mseedat-moj opened this issue Oct 18, 2023 · 0 comments · Fixed by #12294
Closed

Style/RedundantDoubleSplatHashBraces incorrect Autofix #12293

mseedat-moj opened this issue Oct 18, 2023 · 0 comments · Fixed by #12294
Labels

Comments

@mseedat-moj
Copy link

When upgrading to rubocop 1.57.1, splat arguments can be flagged by the Style/RedundantDoubleSplatHashBraces. Running rubocop -A for autofixing the problem introduced incorrect code.


Expected behavior

Autofix should not try to simplifiy code that has list operations on a part of the argument list, in the example below .compact_blank should be applied to the resulting merge hash.

Actual behavior

Given the following code:

def client
  @client ||= Aws::S3::Client.new(
     **{ endpoint: }.merge(default_client_cfg).compact_blank
  )
end

Then running:

rubocop -A --debug

Results in:

<path:linenumber>: C: [Corrected] Style/RedundantDoubleSplatHashBraces: Remove the redundant double splat and braces, use keyword arguments directly.
            **{ ...
            ^^^

And the new code:

def client
  @client ||= Aws::S3::Client.new(
    endpoint:, **default_client_cfg.compact_blank
  )
end

Steps to reproduce the problem

You can run code similar to the above to reproduce the problem.

You can see the Autofix output from Rubocop is not functionally equivalent to the original by running the following code snippet in Rails console/ IRB:

class Rubbish
  def initialize(**args)
    puts args.inspect
  end
end
def endpoint; @endpoint; end;

@endpoint = 'https'
default_client_cfg = {:name=>"mo", :last=>nil, :endpoint=>""}


# Original:
Rubbish.new( **{endpoint:}.merge(default_client_cfg).compact_blank )
# Result > {:name=>"mo"}

# Rubocop autofix:
Rubbish.new( endpoint:, **default_client_cfg.compact_blank )
# Result > {:endpoint=>"http", :name=>"mo"}

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ [bundle exec] rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.2.2) [x86_64-darwin22]
  - rubocop-performance 1.19.1
  - rubocop-rails 2.21.2
  - rubocop-rspec 2.24.1
@koic koic added the bug label Oct 18, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 18, 2023
…latHashBraces`

Fixes rubocop#12293.

This PR fixes a false positive for `Style/RedundantDoubleSplatHashBraces`
when using double splat hash braces with `merge` and method chain.
koic added a commit that referenced this issue Oct 19, 2023
…dundant_double_splat_hash_braces

[Fix #12293] Fix a false positive for `Style/RedundantDoubleSplatHashBraces`
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue Nov 1, 2023
https://build.opensuse.org/request/show/1121572
by user dancermak + dimstar_suse
- updated to version 1.57.2
  ## 1.57.2 (2023-10-26)
  ### Bug fixes
  * [#12274](rubocop/rubocop#12274): Fix a false positive for `Lint/Void` when `each`'s receiver is an object of `Enumerator` to which `filter` has been applied. ([@koic][])
  * [#12291](rubocop/rubocop#12291): Fix a false positive for `Metrics/ClassLength` when a class with a singleton class definition. ([@koic][])
  * [#12293](rubocop/rubocop#12293): Fix a false positive for `Style/RedundantDoubleSplatHashBraces` when using double splat hash braces with `merge` and method chain. ([@koic][])
  * [#12298](rubocop/rubocop#12298): Fix a false positive for `Style/RedundantParentheses` when using a parenthesized hash literal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants