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

Mark Style/HashExcept as unsafe #11163

Merged
merged 1 commit into from Nov 8, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Nov 8, 2022

I think it would be better to assume that Style/HashExcept is unsafe because it detects and replaces even things that should not be replaced.

For example:

invalid_values = [3, 5]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each_slice(2).reject do |value1, _value2|
  invalid_values.include?(value1)
end
#=> [[1, 2], [7, 8], [9, 10]]

The above code is currently auto-corrected as follows:

invalid_values = [3, 5]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each_slice(2).except(*invalid_values)
#=> undefined method `except' for #<Enumerator: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:each_slice(2)> (NoMethodError)

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.

@r7kamura r7kamura force-pushed the feature/style-hash-except-unsafe branch from 9d3e093 to 4c5b303 Compare November 8, 2022 03:02
@koic koic merged commit efb1e62 into rubocop:master Nov 8, 2022
@koic
Copy link
Member

koic commented Nov 8, 2022

Thanks!

koic added a commit to rubocop/rubocop-rails that referenced this pull request Nov 8, 2022
Follow up rubocop/rubocop#11163.

This commit fixes the following build error:

```console
% bundle exec rspec spec/rubocop/cli/autocorrect_spec.rb
(snip)

Failures:

  1) RuboCop::CLI --autocorrect corrects `Style/HashExcept` with `TargetRubyVersion: 2.0`
     Got 2 failures from failure aggregation block.
     # ./spec/support/cli_spec_behavior.rb:25:in `block (2 levels) in <top (required)>'

     1.1) Failure/Error: expect(cli.run(['-a', '--only', 'Style/HashExcept'])).to eq(0)

            expected: 0
                 got: 1

            (compared using ==)
          # ./spec/rubocop/cli/autocorrect_spec.rb:54:in `block (2 levels) in <top (required)>'

     1.2) Failure/Error:
            expect(File.read('example.rb')).to eq(<<~RUBY)
              {foo: 1, bar: 2, baz: 3}.except(:bar)
            RUBY

            expected: "{foo: 1, bar: 2, baz: 3}.except(:bar)\n"
                 got: "{foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }\n"

            (compared using ==)

            Diff:
            @@ -1 +1 @@
            -{foo: 1, bar: 2, baz: 3}.except(:bar)
            +{foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }

          # ./spec/rubocop/cli/autocorrect_spec.rb:55:in `block (2 levels) in <top (required)>'

Finished in 0.28529 seconds (files took 1.12 seconds to load)
3 examples, 1 failure
```
@r7kamura r7kamura deleted the feature/style-hash-except-unsafe branch November 8, 2022 05:10
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