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

False positive on Hash#reject with first numbered parameter #10864

Closed
pnomolos opened this issue Aug 3, 2022 · 0 comments · Fixed by #10865
Closed

False positive on Hash#reject with first numbered parameter #10864

pnomolos opened this issue Aug 3, 2022 · 0 comments · Fixed by #10865
Labels

Comments

@pnomolos
Copy link

pnomolos commented Aug 3, 2022

When using Hash#reject against the first numbered parameter, Rubocop incorrectly suggests to use the &:{method} syntax.

Ex. It suggests some_hash.reject(&:nil?) when the code in question is some_hash.reject { _1.nil? }

This is also an auto-correctable offense, which means it could break working code.

I haven't tested, but my assuption is that there is similar incorrect behaviour for a whole class of enumberable methods such as select, any, etc.


Expected behavior

Inspecting 1 file
.

1 file inspected, no offenses detected

Actual behavior

For /home/pnomolos/Sites/[redacted]: configuration from /home/pnomolos/Sites/[redacted]/.rubocop.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.11.5/config/default.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.11.5/config/default.yml
Default configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-1.32.0/config/default.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.12.4/config/default.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.12.4/config/default.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-rspec-2.5.0/config/default.yml
configuration from /home/pnomolos/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/rubocop-rspec-2.5.0/config/default.yml
Use parallel by default.
Running parallel inspection
Inspecting 1 file
Scanning /home/pnomolos/Sites/[redacted]/tmp.rb
Loading cache from /home/pnomolos/.cache/rubocop_cache/f4becb0ca8ba50e7c0139a428b35b99bc57d5e44/6d7a3b621ca1730e04accd938619e4bdab66cfb1/fc152d4a3b7e3fe6d5dc676df132cb5e76d9c697
C

Offenses:

tmp.rb:1:39: C: [Correctable] Style/SymbolProc: Pass &:nil? as an argument to reject instead of a block.
{ :a => 1, :b => 2, nil => 3 }.reject { _1.nil? }
                                      ^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable
Finished in 0.5734723610803485 seconds

Steps to reproduce the problem

Create tmp.rb as follows:

{ :a => 1, :b => 2, nil => 3 }.reject { _1.nil? }

Run rubocop tmp.rb

RuboCop version

$ bundle exec rubocop -V
1.32.0 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 2.7.5 x86_64-linux)
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.4
  - rubocop-rspec 2.5.0
@koic koic added the bug label Aug 4, 2022
koic added a commit to koic/rubocop that referenced this issue Aug 4, 2022
Fixes rubocop#10864.

This PR fixes a false positive for `Style/SymbolProc` when using `Hash#reject`.
The same is true for `Haash#select`.

```ruby
{foo: :bar}.reject { p _1 } # `_1` is `:foo`
{foo: :bar}.select { p _1 } # `_1` is `:foo`
{foo: :bar}.detect { p _1 } # `_1` is `[:foo, :bar]`
{foo: :bar}.map { p _1 }    # `_1` is `[:foo, :bar]`
```

It fixes `on_block` method because the same issue occurs in normal blocks.

```ruby
{foo: :bar}.select {|item| item.nil? } #=> no erros.
{foo: :bar}.select(&:nil?)             #=> wrong number of arguments (given 1, expected 0) (ArgumentError)
```

`Style/SymbolProc` is already marked as unsafe, but it avoids avoidable issues.
bbatsov pushed a commit that referenced this issue Aug 4, 2022
Fixes #10864.

This PR fixes a false positive for `Style/SymbolProc` when using `Hash#reject`.
The same is true for `Haash#select`.

```ruby
{foo: :bar}.reject { p _1 } # `_1` is `:foo`
{foo: :bar}.select { p _1 } # `_1` is `:foo`
{foo: :bar}.detect { p _1 } # `_1` is `[:foo, :bar]`
{foo: :bar}.map { p _1 }    # `_1` is `[:foo, :bar]`
```

It fixes `on_block` method because the same issue occurs in normal blocks.

```ruby
{foo: :bar}.select {|item| item.nil? } #=> no erros.
{foo: :bar}.select(&:nil?)             #=> wrong number of arguments (given 1, expected 0) (ArgumentError)
```

`Style/SymbolProc` is already marked as unsafe, but it avoids avoidable issues.
WJWH pushed a commit to WJWH/rubocop that referenced this issue Aug 8, 2022
Fixes rubocop#10864.

This PR fixes a false positive for `Style/SymbolProc` when using `Hash#reject`.
The same is true for `Haash#select`.

```ruby
{foo: :bar}.reject { p _1 } # `_1` is `:foo`
{foo: :bar}.select { p _1 } # `_1` is `:foo`
{foo: :bar}.detect { p _1 } # `_1` is `[:foo, :bar]`
{foo: :bar}.map { p _1 }    # `_1` is `[:foo, :bar]`
```

It fixes `on_block` method because the same issue occurs in normal blocks.

```ruby
{foo: :bar}.select {|item| item.nil? } #=> no erros.
{foo: :bar}.select(&:nil?)             #=> wrong number of arguments (given 1, expected 0) (ArgumentError)
```

`Style/SymbolProc` is already marked as unsafe, but it avoids avoidable issues.
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